我的應用有以下型號的財產的總和:Ember.js:計算所有子模型
App.Store = DS.Store.extend({
revision: 11,
adapter: 'DS.FixtureAdapter'
});
App.List = DS.Model.extend({
name: DS.attr('string'),
users: DS.hasMany('App.User'),
tweetsUnread: function(){
/////////////////////////////////////////
// Code to dynamically calculate the sum
// of tweetsUnread property of all
// App.User that are related to this list
/////////////////////////////////////////
}
});
App.User = DS.Model.extend({
screenName: DS.attr('string'),
tweets: DS.hasMany('App.Tweet'),
tweetsUnread: function(){
// TODO: check if this is the correct way to do it
return this.get('tweets').get('length');
}.property('[email protected]'),
list: DS.belongsTo('App.List')
});
App.Tweet = DS.Model.extend({
text: DS.attr('string'),
user: DS.belongsTo('App.User')
});
如何計算所有App.User.tweetsUnread的總和,並使其自動更新應用程序.List.tweetsUnread?
謝謝,減少的伎倆(_edited有點添加ŧ他正確params_) – 2013-04-12 19:13:20
在最近版本的灰燼(至少作爲〜1.1),初始值是用於減少方法中的第二個參數。回調是第一個。 http://emberjs.com/api/classes/Ember.Enumerable.html#method_reduce – joelcox 2013-11-14 11:05:51
THX此提示。我已經相應地更新了這篇文章。 – mavilein 2013-11-14 16:03:30