覆盖Odoo 10中的JavaScript函数

时间:2019-04-13 08:46:02

标签: odoo odoo-10

我正在尝试覆盖js函数ActionpadWidget。 这是原始代码:

var ActionpadWidget = PosBaseWidget.extend({
    template: 'ActionpadWidget',
    init: function(parent, options) {
        var self = this;
        this._super(parent, options);

        this.pos.bind('change:selectedClient', function() {
            self.renderElement();
        console.log("Payment Click");
        // console.log("Payment Click");
        // console.log("Payment Click");
        });
    },
    renderElement: function() {
        var self = this;
        this._super();
        this.$('.pay').click(function(){
            var order = self.pos.get_order();
            var has_valid_product_lot = _.every(order.orderlines.models, function(line){
                return line.has_valid_product_lot();
            });
            if(!has_valid_product_lot){
                self.gui.show_popup('confirm',{
                    'title': _t('Empty Serial/Lot Number'),
                    'body':  _t('One or more product(s) required serial/lot number.'),
                    confirm: function(){
                        self.gui.show_screen('payment');
                    },
                });
            }else{
                console.log("orginal elseeeeeeeeeeeeeeeeeeeeeeee");
                console.log("orginal elseeeeeeeeeeeeeeeeeeeeeeee");
                console.log("orginal elseeeeeeeeeeeeeeeeeeeeeeee");
                self.gui.show_screen('payment');
            }
        });
        this.$('.set-customer').click(function(){
            self.gui.show_screen('clientlist');
        });
    }
});

实际上,我想完全替换renderElement函数。

所以我这样尝试:

screens.ActionpadWidget.include({

    renderElement: function(){
        console.log('Am here',screens.ActionpadWidget.prototype);
        this._super();
        self = this;
        this.$('.pay').click(function(){
            console.log('Am here tooo!!!')
            var order = self.pos.get_order();
            var has_valid_product_lot = _.every(order.orderlines.models, function(line){
                return line.has_valid_product_lot();
            });
            if(!has_valid_product_lot){
                self.gui.show_popup('confirm',{
                    'title': _t('Empty Serial/Lot Number'),
                    'body':  _t('One or more product(s) required serial/lot number.'),
                    confirm: function(){
                        self.gui.show_screen('payment');
                    },
                });
            }else{
                // console.log("orginal elseeeeeeeeeeeeeeeeeeeeeeee");
                // console.log("orginal elseeeeeeeeeeeeeeeeeeeeeeee");
                console.log("Sample");
                self.gui.show_screen('payment');
            }
        });

        // this._super();
    },
});

以上代码成功运行,但仅在执行原始代码后才能运行。 我不想执行原始代码。

注意:  我在上面的代码中评论了this._super。那时Payment and Customer的按钮没有显示。 我该如何实现?

2 个答案:

答案 0 :(得分:1)

下面的行称为继承函数。

 function cal() {
    var numZero5 = document.getElementById('num05').value;
    var numZero6 = document.getElementById('num06').value;
    var numZero7 = document.getElementById('num07').value;
    var total3 = parseInt(numZero5) * parseInt(numZero6) * parseInt(numZero7) * .7854 * 1.5;
    var p = document.getElementById('total3');
    p.innerHTML += total3;
  }

用以下代码替换该行:

this._super(); 

还可能需要添加下面一行。

PosBaseWidget.prototype.renderElement.call(this);

完整代码:

  var PosBaseWidget = require('point_of_sale.BaseWidget');

答案 1 :(得分:0)

  renderElement: function() {
    var self = this;
    this._super();
    this.$('.pay').click(function(){
           var order = self.pos.get_order();
        var has_valid_product_lot = _.every(order.orderlines.models, function(line){
            return line.has_valid_product_lot();
        });
        if(!has_valid_product_lot){
           //solution
           self.gui.show_screen('products');
           //end solution
            self.gui.show_popup('confirm',{
                'title': _t('Empty Serial/Lot Number'),
                'body':  _t('One or more product(s) required serial/lot number.'),
                confirm: function(){
                    self.gui.show_screen('payment');
                },
            });
        }
        else
        {
            self.gui.show_screen('payment');
        }
    });
    this.$('.set-customer').click(function(){
        self.gui.show_screen('clientlist');
    });
}