简体   繁体   中英

Ember.js computed property with ArrayController

I have an Ember.js ArrayController with a model of People. I'm trying to create a computed 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}}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM