javascript - Ember.js computed property with ArrayController - Stack Overflow

I have an Ember.js ArrayController with a model of People. I'm trying to create a puted property t

I have an Ember.js ArrayController with a model of People. I'm trying to create a puted property that generates the average of people's weight. It seems like this should be fairly easy, but I'm stuck. Here's my code.

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')
});

Handlebars code.

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

Average weight: {{weight}}

I have an Ember.js ArrayController with a model of People. I'm trying to create a puted property that generates the average of people's weight. It seems like this should be fairly easy, but I'm stuck. Here's my code.

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')
});

Handlebars code.

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

Average weight: {{weight}}
Share Improve this question asked Jun 20, 2013 at 18:10 Jarrod NettlesJarrod Nettles 6,3036 gold badges29 silver badges47 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Figured it out. For some reason you need to use 'content.@each' to access the model data inside the puted property.

averageWeight: function(val) {
    var weights = this.get('[email protected]').toArray(); //this is the critical part!

    weights.forEach(function(val)) {
        //calc average here
    }

    return average;
}.property('@each.weight')

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745606310a4635692.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信