Skip to content

Instantly share code, notes, and snippets.

@poteto
Last active October 1, 2015 08:49
Show Gist options
  • Save poteto/f1277ab27598b1cbef75 to your computer and use it in GitHub Desktop.
Save poteto/f1277ab27598b1cbef75 to your computer and use it in GitHub Desktop.
import Ember from 'ember';
import DS from 'ember-data';
var computed = Ember.computed;
var get = Ember.get;
var RSVP = Ember.RSVP;
export default DS.Model.extend({
name: DS.attr('string'),
players: DS.hasMany('player', { async: true }),
coach: DS.belongsTo('coach', { async: true }),
coachScore: computed.alias('coach.score'),
playersScore: computed('[email protected]', function() {
var promise = get(this, 'players')
.then(function(players) {
return RSVP.all(players.mapBy('score'))
})
.then(function(scores) {
return scores.reduce(function(previous, current) {
return previous + current;
}, []);
});
return DS.PromiseObject.create({ promise: promise });
}),
score: computed('playersScore', 'coachScore', function() {
var playersScore = get(this, 'playersScore');
var coachScore = get(this, 'coachScore');
return playersScore * coachScore;
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment