Help

5

How to extend a function of models.js

Avatar
Ameera PP

Hello everyone,

There is a function named 'set_discount' in /point_of_sale/static/src/js/models.js. I want to extend that function. I tried to use '.include', but it failed.  Please help me to find a solution. 

2 Comments
Avatar
Discard
Avatar
Axel Mendoza
-

In what odoo version? js change a lot from version to version

Avatar
Ameera PP
-

version 9

2 Answers
0
Avatar
walid mashal
Best Answer

It's probably late for an answer but if any one wants to do something like this, they can do the following to achieve this purpose. I have just added the code of odoo source but you can replace the code inside set_discount with any code you want and do what you need to do.
create a .js file under your static > src > js folder and add the following code to it:

odoo.define('pos_discounts_question', function (require) {
    "use strict";
    var pos_models = require('point_of_sale.models');
    var custom_models = pos_models.Orderline.extend({   
        set_discount: function(discount){
            var disc = Math.min(Math.max(parseFloat(discount) || 0, 0),100);
            this.discount = disc;
            this.discountStr = '' + disc;
            this.trigger('change',this);
        },
    });
    pos_models.Orderline = custom_models;
});

make sure you add this .js file to the web.assets_backend

Avatar
Discard
0
Avatar
Bernardo Kyotoku
Best Answer

Part of point_of_sale are extended from web.Class that has the include function. But the model in models.js are extended from Backbone.Model which don't. I assume the Odoo people didn't have time to get at it. Anyway, my workaround was using the prototype properties in Backbone.Model.

Suppose you want to modify the function foo from Model_of_interest. First rename the foo to old_foo using the prototype. Then create the new foo function, you can use the old foo function within the new foo if you (probably) need.

Model_of_interest.prototype.old_foo = model_of_interest.prototype.foo;
Model_of_interest.prototype.foo = function(args){ return this.old_foo(args)}

I don't know the problems of this method, but it worked for me.

Avatar
Discard