Search code examples
javascriptextendodoo-12

how to extend the initialize function in POS models.js


i want to add variable in the initialize function but i dont know how to do it properly

here is what i have tried right now

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);
        },

i've tried doing what is said here but the example is for the PosModel not Order

here is the error

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

Solution

  • You do not need var self = this; as it is not used later on.

    Also, you most likely need to return something from the initialize function.

    _super_order.initialize.apply(this,arguments);

    Should be:

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