2016-06-23 68 views
1

我試圖通過選擇一個電影的個別分數,並找到平均值來計算屬性的工作。這有點棘手,但這是我迄今爲止......Ember找到關鍵元素的平均值

testing: Ember.computed('movie.title', 'movie.rating', function() { 
    return this.get('movie.title') + ', ' + this.get('movie.rating'); 
}), 

averageScore: Ember.computed('[email protected]', function() { //Used to determine each score of review 
    return this.get('review').(function(sum, review) { 
    return sum += review.get('score'); 
    }, 0)/this.get('reviews').get('length'); 
}), 

我知道我錯過了什麼,有什麼建議嗎?

回答

1

sum是內置的:

scores:  Ember.computed.mapBy('review', 'score'), 
sumOfScores: Ember.computed.sum('scores'), 
averageScore: Ember.computed('sumOfScores', 'scores.length', function() { 
    return this.get('sumOfScores')/this.get('scores.length'); 
}