How to update model object in every loop

I tried to update every object in my model in every loop

I want to add a property size

to every object in the model.

But it doesn't seem to work.

CONSOLE (it didn't work after I added the size property)

self.model().get(i).size=123
>> 123

self.model().get(i)
>>
id: 1
name: "URL_Command_Comparison"
rounds: 2
team: "cvt"
__proto__: Object

      

CODE

  model: function(){
    return Ember.A([
      {id: 1, name: 'URL_Command_Comparison', 
        team: 'cvt'
      },
      {id: 2, name: 'Auto Test',
        'manualHours':20 ,'autoHours': 3, 
      ....
$.each(this.model(),function(i,d){
  self.model().get(i).set("size",self.perRoundROI(d)*d.rounds*d.coverage)
});

      

+3


source to share


1 answer


First, the model hook should probably never be called by you ( this.model()

). It is called by the router when it creates the context for the current url. If you want to access the model from the route after completing the transition, you can use this.currentModel

. If you want to access it somewhere else during the transition, it is usually passed to generic hooks ( afterModel

, setupController

). http://emberjs.com/api/classes/Ember.Route.html#method_afterModel If you want to access it from a deeper nested route you can use this.modelFor('foo')

where foo

is another route name.

In your specific case, I would wrap your objects in an Ember object and create a computed property that calculates this for you. It's cool if you change any of the other properties the computed property depends on, it will recalculate.

An object

App.CoolObject = Em.Object.extend({
  size: function(){
    return this.get('rounds') * this.get('coverage'); // or whatever it is you want here
  }.property('rounds', 'coverage')
});

      



Route

model: function(){
    return Ember.A([
      {id: 1, name: 'URL_Command_Comparison', 
        team: 'cvt'
      },
      {id: 2, name: 'Auto Test',
        'manualHours':20 ,'autoHours': 3, 
      }]);
},

setupController: function(controller, model){
  model = model.map(function(item){
    return App.CoolObject.create(item);
  });
  this._super(controller, model);
}

      

Example: http://emberjs.jsbin.com/nupoko/1/edit

+2


source







All Articles