添加另一个元素到模型列表[odoo-8] pos javascript

时间:2017-11-25 21:00:01

标签: javascript openerp odoo-8 odoo-9 odoo-10

在pos javascript中有这段代码:

   module.PosModel = Backbone.Model.extend({
       .....
       .....
      models: [
            {
                model:  'res.users',
                fields: ['name','company_id'],
                ids:    function(self){ return [self.session.uid]; },
                loaded: function(self,users){ self.user = users[0]; },
            },
             ....
             ....
          ]

在我的costum模块中,我只想在列表的末尾添加一个元素, 我设法添加它:

           module.PosModel = module.PosModel.extend({
               models: [
                    {
                        model:  'res.users',
                        fields: ['name','company_id'],
                        ids:    function(self){
                        return [self.session.uid];
                         },
                        loaded: function(self,users){ self.user = users[0]; },
                    },
                    .....
                    // repeate the same list with my new element 
                  ],
               }

现在我的问题是如何将我的元素添加到旧列表而不必重复孔列表。

1 个答案:

答案 0 :(得分:2)

我们有权访问initialize方法中的所有属性:

    // in needed to save prototype here
    // so it will not cause a recursive loop
    var _super = module.PosModel.prototype;
    module.PosModel = module.PosModel.extend({
     initialize: function (session, attributes) {
        // call super to set all properties
        _super.initialize.apply(this, arguments);
        // here i can access the models list like this and add an element.
        this.models.push({
        // load allowed users
            model:  'res.users',
            fields: ['name'],
            domain: function(self){ return [['id','in',self.config.user_ids]]; },
            loaded: function(self,users){
                console.log(users);
                self.allowed_users = users;
            },
        })
        return this;
     },

    });