콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
4 답글
10297 화면

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. 

아바타
취소

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

작성자

version 9

베스트 답변

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

아바타
취소
베스트 답변

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.

아바타
취소