2

我有一个带有 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}}
4

1 回答 1

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')
于 2013-06-20T18:28:45.713 回答