javascript - how to update model's object in each loop -
i tried update each object in model within each loop
i wanna add size
property each object in model.
but seems doesn't work.
console (it didn't work after appended 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) });
firstly model hook should never called (this.model()
). it's called router when it's building context current url/transition. if want access model route after transition has completed can use this.currentmodel
. if want access somewhere else in pipeline of transition passed in common hooks (aftermodel
, setupcontroller
). http://emberjs.com/api/classes/ember.route.html#method_aftermodel if want access deeper nested route, can use this.modelfor('foo')
foo
other route's name.
in particular case, i'd wrap objects in ember object , create computed property calculates you. cool thing that, if change of other properties computed property depends on, recalculate.
object
app.coolobject = em.object.extend({ size: function(){ return this.get('rounds') * this.get('coverage'); // or whatever 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); }
Comments
Post a Comment