Ember.js: Calculate the sum of a property of all child models

前端 未结 3 1383
醉梦人生
醉梦人生 2021-02-07 10:57

My application has the following models:

App.Store = DS.Store.extend({
    revision: 11,
    adapter: \'DS.FixtureAdapter\'
});

App.List = DS.Model.extend({
            


        
3条回答
  •  迷失自我
    2021-02-07 11:29

    Another option would be to use Ember.computed.sum see here

    App.List = DS.Model.extend({
      name: DS.attr('string'),
      users: DS.hasMany('App.User'),
    
      tweetsUnread: Ember.computed.mapBy('users', 'tweetsUnread'),
      totalTweetsUnread: Ember.computed.sum('tweetsUnread')   
    });
    

提交回复
热议问题