for loop - EmberJS: how to use nested objects attributes in computed properties? -


i have trouble understanding proper way call attributes of child in nested object model, , use them compute new property. here i'm trying far:

monitor.processcontroller = ember.objectcontroller.extend({   nbusers: function() {     var total = 0;     this.get('containers').then(function(containers) {             containers.foreach(function(container, index){                total += container.get('nbusers');             });         });     return total;   }.property('containers.@each.nbusers') });  monitor.containercontroller = ember.objectcontroller.extend();  monitor.process = ds.model.extend({   containers: ds.hasmany('container', {async : true}),   name: ds.attr('string') });  monitor.container = ds.model.extend({   process: ds.belongsto('process'),   name: ds.attr('string'),   nbusers: ds.attr('integer') });  // test data  monitor.process.fixtures = [  {   id: 1,   name: 'mumble',   containers: [1,2]  },  {   id: 2,   name: 'eve',   containers: [3]  } ];  monitor.container.fixtures = [  {   id: 1,   name: 'first',   process: 1,   nbusers: 1  },  {   id: 2,   name: 'second',   process: 1,   nbusers: 1  },  {   id: 3,   name: 'unique',   process: 2,   nbusers: 1  } ]; 

so process has multiple child containers, , compute number of total users each process, based on containers data, dynamically. sounds trivial, apparently not.

{{nbusers}} keeps returning "0". pointers on i'm doing wrong?

you're property asynchronous, return 0 before ever update total, , since it's not reference being passed back, it's not update value that's been returned. property total gets updated, not value returned computed property.

the easiest way change it, make observer, , have update property.

monitor.processcontroller = ember.objectcontroller.extend({   nbusers: 0,   nbuserswatcher: function() {     var self = this;     this.get('containers').then(function(containers) {       var total = 0;       containers.foreach(function(container, index){         total += container.get('nbusers');       });       self.set('nbusers', total);     });   }.observes('containers.@each.nbusers') }); 

you might able use computed well, i'd have test before tell that's possible.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -