So I'm new to Ember and trying to work out the best way to set a computed property that depends upon related model data. I'll include the models below FYI.
I want to set the computed property to an asynchronous result (desiredOutcome). My question is how do I this? Or is it more appropriate to use an observer, as in this question.
score: function() {
var self = this;
var allscores = this.get('model.scores');
//Find the scoreRecord that has the appropriate objective id (in this case just 1)
var scoreRecord = allscores.find(function(item){
var scoreID = item.get('id');
return self.store.find('score', scoreID).then(function(scoreRecord){
var objID = Number(scoreRecord.get('objective.id'));
if (objID === 1){return true;}
});
});
//Return the scoreRecord's score attribute
var scoreID = scoreRecord.get('id');
return this.store.find('score', scoreID).then(function(score){
var desiredOutcome = score.get('score');
console.log(desiredOutcome);
return desiredOutcome;
});
}.property('[email protected]', 'selectedObjective'),
Apologies for the poor choice of variable names etc...
My models:
student
scores: DS.hasMany('score', {async: true}),
name: DS.attr('string')
objective
name: DS.attr('string'),
scores: DS.hasMany('score', {async : true})
score
scoreResult: DS.attr('number'),
objective: DS.belongsTo('objective', {async: true}),
student: DS.belongsTo('student', {async: true})
-------------Update---------------
JSBin here. So what I'd like is to return one of the scores, the 1 or 2 that are being logged in the console, as the computed property "score". But I'm assuming I'm just returning an unresolved promise - Inexperienced with promises - How do I adapt so that it resolves?
--I've also been thinking it might be better for me take a different approach with what I'm trying to do here. I might look into creating a component with a model of the appropriate "score". I'll update with some details if that works out.
model.scores.@each
instead of[email protected]
? – Kalman