我有一个带有People模型的Ember.js ArrayController。我正在尝试创建一个计算属性,用于生成人体重的平均值。看起来这应该相当容易,但我被卡住了。这是我的代码。
App.PeopleController = Ember.ArrayController.extend({
//each model in the array has a "weight" property
averageWeight: function() {
//I don't know what to do here
}.property('@each.weight')
});
把手代码。
{{#each controller}}
{{name}}
{{/each}}
Average weight: {{weight}}
答案 0 :(得分:8)
想出来。出于某种原因,您需要使用'content。@ each'来访问计算属性中的模型数据。
averageWeight: function(val) {
var weights = this.get('content.@each.weight').toArray(); //this is the critical part!
weights.forEach(function(val)) {
//calc average here
}
return average;
}.property('@each.weight')