Ember data multiplied property not working on nested association

I have a simple datamodel:

a Leg, has a lot of players who have a lot of turns:

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

App.Leg = DS.Model.extend({
  players: DS.hasMany('App.Player'),
  turnCount: function() {
    var i = 0;
    this.get('players').forEach(function(p) {
      i+= p.get('turns.length');
    });
    return i;
  }.property('players.@each.turns.length')

});

App.Player = DS.Model.extend({
  leg: DS.belongsTo('App.Leg'),
  turns: DS.hasMany('App.Turn')
});


App.Turn = DS.Model.extend({
  player: DS.belongsTo('App.Player')
});

      

The computed property is turnCount

not automatically updated when I create a new Turn, but it should, right?

/*
* do this on the console to verify
*/

var leg = App.Leg.createRecord();
var player = leg.get('players').createRecord();

leg.get('turnCount');
// => 0
player.get('turns').createRecord();
leg.get('turnCount')
// => 0

      

UPDATE

It seems that if you stick with one layer of nested things, just work. So:

.property('players.@each.someProperty') 

      

works as long as this someProperty is not Enumerable.

+3


source to share


1 answer


Try the following:

.property('players.@each.turns.@each')

      



You can also use the method reduce

to calculate totals:

turnCount: function() {
  return this.get('players').reduce(function(value, player) {
    return value + player.get('turns.length');
  }, 0);
}.property('players.@each.turns.@each')

      

0


source







All Articles