javascript - 如何扩展 POS models.js 中的初始化函数

标签 javascript extend odoo-12

我想在初始化函数中添加变量,但我不知道如何正确执行

这是我现在尝试过的

odoo.define('tw_pos_inherit_model.attemptInherit', function (require) {
    "use strict";
    var POSInheritmodel = require('point_of_sale.models');

    var _super_order = POSInheritmodel.Order.prototype;

    POSInheritmodel.Order = POSInheritmodel.Order.extend({
        initialize: function(session,attributes){
            var self = this;
            this.additional_discount = 0;
            _super_order.initialize.apply(this,arguments);
        },

我已经尝试按照所说的去做here但这个例子是针对 PosModel 而不是 Order

这是错误

point_of_sale.assets.js:79 Cannot read property 'name' of undefined TypeError: Cannot read property 'name' of undefined
    at Engine.eval (eval at _render (http://localhost:8071/web/content/319-5a1ade2/web.assets_common.js:3416:73), <anonymous>:50:34)
    at Engine._render (http://localhost:8071/web/content/319-5a1ade2/web.assets_common.js:3415:296)
    at Engine.render (http://localhost:8071/web/content/319-5a1ade2/web.assets_common.js:3415:151)
    at Engine._render (http://localhost:8071/web/content/319-5a1ade2/web.assets_common.js:3419:57)
    at Engine.render (http://localhost:8071/web/content/319-5a1ade2/web.assets_common.js:3415:151)
    at Class.render_orderline (http://localhost:8071/web/content/723-a89f195/point_of_sale.assets.js:306:5014)
    at Class.renderElement (http://localhost:8071/web/content/723-a89f195/point_of_sale.assets.js:308:354)
    at Class.replace (http://localhost:8071/web/content/723-a89f195/point_of_sale.assets.js:307:423)
    at Class.start (http://localhost:8071/web/content/723-a89f195/point_of_sale.assets.js:325:810)
    at http://localhost:8071/web/content/319-5a1ade2/web.assets_common.js:3684:52

最佳答案

您不需要var self = this;,因为稍后不会使用它。

此外,您很可能需要从初始化函数返回一些内容。

_super_order.initialize.apply(this,arguments);

应该是:

return _super_order.initialize.apply(this,arguments);

关于javascript - 如何扩展 POS models.js 中的初始化函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61320827/

相关文章:

javascript - 覆盖 JavaScript 函数会引发错误 "Some modules could not be started"和 "web is not defined"

python-3.x - 单击 odoo 12 中的按钮后关闭向导

class - 如何创建 coffeescript 单例子类

javascript - IE7 中的 Element.prototype?

javascript - 如何将正则表达式分配给变量以供以后在字符串上使用?

javascript - JQuery/Javascript 淡入滚动不工作

c# - 如何让 newtonsoft 将 yes 和 no 反序列化为 bool 值

python - 如何根据 sale_order_line 中的自定义日期时间字段更新 stock_move 文档中的 date_expected 值

javascript - 删除在 Jest 中记录原点行

javascript - 带有自定义滚动条(mCustomScrollbar)的自定义选择(Nice select)