ứng dụng của tôi có các mô hình sau:Ember.js: Tính tổng của một tài sản của tất cả các mô hình con
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')
});
Làm thế nào tôi có thể tính toán tổng của tất cả App.User.tweetsUnread và làm cho nó tự động cập nhật ứng dụng .List.tweetsUnread?
Cảm ơn, giảm đã làm các trick (_edited một chút để thêm t ông sửa params_) –
Trong các phiên bản gần đây của Ember (ít nhất là ~ 1.1), giá trị ban đầu là đối số thứ hai cho phương thức giảm. Gọi lại là lần đầu tiên. http://emberjs.com/api/classes/Ember.Enumerable.html#method_reduce – joelcox
Thx cho gợi ý này. Tôi đã cập nhật bài đăng cho phù hợp. – mavilein