最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin10PV0评论

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')
Post a comment

comment list (0)

  1. No comments so far