jeudi 26 février 2015

Ember - async computed property or observer?

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('model.@each.scores', '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})




Aucun commentaire:

Enregistrer un commentaire