Ember.js使用ArrayController计算属性

Jar*_*les 2 javascript ember.js

我有一个Ember.js ArrayController和一个People模型.我正在尝试创建一个计算属性,用于生成人体重的平均值.看起来这应该相当容易,但我被卡住了.这是我的代码.

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')
});
Run Code Online (Sandbox Code Playgroud)

把手代码.

{{#each controller}}
   {{name}}
{{/each}}

Average weight: {{weight}}
Run Code Online (Sandbox Code Playgroud)

Jar*_*les 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')
Run Code Online (Sandbox Code Playgroud)