Search code examples
javascriptxmlbackbone.jsodoo-8odoo

How to modify Backbone JS function in Odoo 8?


I want to modify a section of JS code in Odoo 8 (module point_of_sale). The original code is this one:

function openerp_pos_models(instance, module){
    ...
    module.Order = Backbone.Model.extend({
        ...
        addPaymentline: function(cashregister) {
            var paymentLines = this.get('paymentLines');
            var newPaymentline = new module.Paymentline({},{cashregister:cashregister, pos:this.pos});
            if(cashregister.journal.type !== 'cash'){
                newPaymentline.set_amount( Math.max(this.getDueLeft(),0) );
            }
            paymentLines.add(newPaymentline);
            this.selectPaymentline(newPaymentline);

        },
    });
}

I modified some lines of that code and the changes are working as I want (I only removed the if (cashregister.journal.type !== 'cash') line). The problem is that now, I want to modify this code on a right way, from a module made by me.

To do that, I added a JavaScript file, which is called from the following XML file (this last one is included in my __openerp__.py data parameter):

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
        <template id="assets_backend" name="nmx_pos_ext assets" inherit_id="web.assets_backend">
            <xpath expr="." position="inside">
                <script type="text/javascript" src="/nmx_pos_ext/static/src/js/nmx_pos_ext.js"></script>
            </xpath>
        </template>
    </data>
</openerp>

And the content of the JS file is:

openerp.nmx_pos_ext = function(instance) {

var _t = instance.web._t;

instance.point_of_sale.Order.include({
    addPaymentline: function(cashregister) {
        var paymentLines = this.get('paymentLines');
        var newPaymentline = new module.Paymentline({},{cashregister:cashregister, pos:this.pos});
        newPaymentline.set_amount( Math.max(this.getDueLeft(),0) );
        paymentLines.add(newPaymentline);
        this.selectPaymentline(newPaymentline);

    },
});

}

I updated the module and the changes are not being applied, I get an error because include seems to not exist in Backbone models. I tried with set instead of include, but I get the same error:

instance.point_of_sale.Order.set is not a function

Can anyone help me here, please?


Solution

  • You can try like this way.

    openerp.nmx_pos_ext = function(instance) {
        var _t = instance.web._t;
    
        var _initialize_Order_ = instance.point_of_sale.Order.prototype;
        instance.point_of_sale.Order = instance.point_of_sale.Order.extend({
            initialize: function(attributes){
                _initialize_Order_.initialize.call(this,attributes);
            },
            addPaymentline: function(cashregister) {
                var paymentLines = this.get('paymentLines');
                var newPaymentline = new instance.point_of_sale.Paymentline(
                    {},
                    {
                        cashregister: cashregister,
                        pos: this.pos
                    }
                );
                newPaymentline.set_amount(
                    Math.max(this.getDueLeft(), 0)
                );
                paymentLines.add(newPaymentline);
                this.selectPaymentline(newPaymentline);
    
            },
        });
    }