Search code examples
javascriptodooodoo-11

Patching an existing js function but still show up


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.

enter image description here

How can I replace the origin one totally?


Solution

  • When you overwrite the function's entire content, then you don't need to call this._super.apply(this, arguments); Currently, you are creating two dialogs.

    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.