Created
March 11, 2016 17:55
-
-
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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