简体   繁体   English

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

[英]add another element to models list [odoo-8] pos javascript

in pos javascript there this code: 在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]; },
            },
             ....
             ....
          ]

In my costum module i just want to add one element to the end of the list, I managed to add it doing this: 在我的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 
                  ],
               }

Now my question is how to just add my element to the old list without having to repeate the hole list. 现在,我的问题是如何仅将我的元素添加到旧列表中,而不必重复孔列表。

The good thing that we have access to all attribute in initialize method: 在初始化方法中,我们可以访问所有属性的好处是:

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

    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM