Want to calculate the sum of a property of all child models in EmberJS? This is a more elegant solution using reduce. I have never used it and this isn’t tested but i am quite confident that this should work:
1 2 3 4 5 6 7 8 9 10 | App.List = DS.Model.extend({ name: DS.attr('string'), users: DS.hasMany('App.User'), tweetsUnread: function(){ var users = this.get("users"); return users.reduce(0, function(previousValue, user){ return previousValue + users.get("tweetsUnread"); }); }); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.