How to handle multiple changes to the main trunk model in one handler

I have a view and a model associated with it in the highway. Review the observations to change the model and change its display area accordingly. eg:

var Part = Bacbone.Model.extends({
  defaults:{
    partId = null,
    manufacturer: null,
    manufactureDate: null,
    type: null
  }
});

var PartsCollection = Backbone.Collection.extends({
   model:Part;
)};

var Car = Backbone.Model.extends({
   defaults:{
       carModel: null,
       carName: null,
       color: null,
       partsCollection: null
   },  
   //Overwite the parse method to fill partsCollection
   parse: function(response){
       // creating partsCollection from response and adding an attribute
       // response.partsCollection = new PartsCollection();
       retrun response;
   }
});

I have a structure similar to the one above. In my development strategy, I change the contents of the view when the model changes.

So now, for example, if I replace manufacturer "A" with manufacturer "B" for 1000 parts of 5000 parts. This should change my mind, and for this I listen to the event of the change of the model in my opinion. A modification of 1000 parts will result in 1000 event changes.

"manufacturerDate" Part, attor manufacturerDate, , , 1000 .

, . - ?

+4
2

, , . CarView . CarView change:manufacturer this.model.partsCollection. API changeManufacturer , ( ) . API change:manufacturer , , API- changeManufacturer, CarView change:manufacturer .

var CarView = Backbone.View.extend({
    initialize: function() {
        // do other stuffs
        this.listenTo(this.model.partsCollection, "change:manufaturer", manufacturereChanged);

    },

    manufacturereChanged: function(arrOfChangedParts) {
        // you now have all changed parts array
        // process as your logic
    }
});


var PartsCollection = Backbone.Collection.extend({
   model: Part,
   changeManufacturer: function(arrPartIds, newManfacturerDetails) {
      var arrChangedModels = [];
      // iterate over ids
          // get the model and change manufacturer info in model
          // add changed model to arrChangedModels
      // ends loop

      // once all models are changed trigger an event "change:manufacturer"
      this.trigger('change:manufacturer', arrChangedModels)
   }
});
0

, , , .

n-.

0

All Articles