Patching an existing js function but still show up

天大地大妈咪最大 提交于 2020-08-10 19:14:29

问题


I use odoo11. I have a requirement that is to change the confirm content when click delete.

I found this from a js file called basic_controller.js on web addons

var BasicController = AbstractController.extend(FieldManagerMixin, {
...
    _deleteRecords: function (ids) {
        var self = this;
        function doIt() {
            return self.model
                .deleteRecords(ids, self.modelName)
                .then(self._onDeletedRecords.bind(self, ids));
        }
        if (this.confirmOnDelete) {
            Dialog.confirm(this, _t("Are you sure you want to delete this record ?"), {
                confirm_callback: doIt,
            });
        } else {
            doIt();
        }
    },
...

And I found the patching method from here. I write a new js file to patch it like this.

BasicController.include({
    _deleteRecords: function (ids) {
        this._super.apply(this, arguments);
        var self = this;
        function doIt() {
            return self.model.deleteRecords(ids, self.modelName).then(self._onDeletedRecords.bind(self, ids));
        }
        if (this.confirmOnDelete) {
            Dialog.confirm(this, _t("rewrite content?"), {
                confirm_callback: doIt,
            });
        } else {
            doIt();
        }
    },

})

It will show my content but after confirm or cancel. The origin one will follow up.

How can I replace the origin one totally?


回答1:


if you overwrite functions whole content. Then you don't need to call this._super.apply(this, arguments); Currently it creates one dialog before yours.

JS File

odoo.define('mytest.field', function (require) {
'use strict';

var basic_fields = require('web.BasicController');
var Dialog = require('web.Dialog');
var core = require('web.core');

var _t = core._t;

var FieldClearbit = basic_fields.include({
    _deleteRecords: function (ids) {
        var self = this;
        function doIt() {
            return self.model.deleteRecords(ids, self.modelName).then(self._onDeletedRecords.bind(self, ids));
        }
        if (this.confirmOnDelete) {
            Dialog.confirm(this, _t("rewrite content?"), {
                confirm_callback: doIt,
            });
        } else {
            doIt();
        }
    },

})

});

XML File

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <template id="assets_backend" name="Rdets" inherit_id="web.assets_backend">
        <xpath expr="." position="inside">
            <script type="text/javascript" src="/mytest/static/src/js/test.js" />
        </xpath>
    </template>

</odoo>

Try enabling assets debugging to regenerate web assets. Most likely you need to upgrade your module to changes to apply.



来源:https://stackoverflow.com/questions/63050344/patching-an-existing-js-function-but-still-show-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!