Search code examples
javascriptodooodoo-8odoo-9odoo-10

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


in pos javascript there this code:

   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:

           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.


Solution

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