Skip to content

Instantly share code, notes, and snippets.

@courtneyphillips
Created March 11, 2016 17:55
Show Gist options
  • Save courtneyphillips/75c25ae2ba1a04066fde to your computer and use it in GitHub Desktop.
Save courtneyphillips/75c25ae2ba1a04066fde to your computer and use it in GitHub Desktop.
Ember.js computed property averaging value on `hasMany` Firebase relationship in model.
//this is the model for an object that has many reviews. reviews each contain a score. the averageScore property below gathers
// all reviews, sums all scores from all reviews, and divides by the number of reviews to calculate an average score.
import Ember from 'ember';
import DS from 'ember-data';
export default DS.Model.extend({
reviews: DS.hasMany('review', {async: true}),
...
averageScore: Ember.computed('[email protected]', function() {
return this.get('reviews').reduce(function(sum, review) {
return sum += review.get('rating');
}, 0) / this.get('reviews').get('length');
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment