CriticalImpact
CriticalImpact

Reputation: 690

Access has Many via foreach in computed property in ember

So I've got a hasMany defined as so
quests: DS.hasMany('quest')
I have a property that is supposed to go through each quest and work out the total. The function looks like this,

questXP: function() {
    var amount = 0;

    this.get('quests').forEach(function(item)
    {
        console.log(item.get('xpReward'));
        amount += parseInt(item.get('xpReward'));
    });
    return amount;
}.property('quests'),  

I've tried adding async: true to the hasMany but it stops the forEach from working at all. At the moment it loops 3 times(I have 3 quests) but it isn't able to access any of the quest's properties. My thought is it's related to the fact that the quests are still being loaded.

Any ideas on what I'm doing wrong?

Upvotes: 4

Views: 807

Answers (1)

Marcio Junior
Marcio Junior

Reputation: 19128

Your computed property depends of each xpReward property. So you need to use [email protected] istead of quests.

questXP: function() {
    var amount = 0;

    this.get('quests').forEach(function(item)
    {
        console.log(item.get('xpReward'));
        amount += parseInt(item.get('xpReward'));
    });
    return amount;
}.property('[email protected]'),

Upvotes: 4

Related Questions