mercredi 21 janvier 2015

record not loaded on time to compute property in a component?

Component:



{{foods-table foods=model}}


receives foods attribute from outer scope.


This component has a computed property:



saturatedSum: function() {

var sum = this.get('foods').reduce(function(previousValue, food, index){
return previousValue + food.get("saturated");
}, 0);

return sum;

}.property('foods.@each') //key @each instructs to update bindings






I'm using this component twice, once in meals route and once in meals.findFood route. First component instance renders {{saturatedSum}} as NAN and second component gives appropriate numerical sum.


I'm assuming record associated with attribute passed from outer scope is not fully loaded before reduction (this.get('foods').get('length') will return 4, but this.get('foods').objectAt(0).get('saturated') will be undefined.


What is the elegant way to deal with this?





Aucun commentaire:

Enregistrer un commentaire