【发布时间】:2015-04-28 23:21:46
【问题描述】:
所以我是 Ember 的新手,并试图找出设置取决于相关模型数据的计算属性的最佳方法。我将包括以下模型供参考。
我想将计算属性设置为异步结果(desiredOutcome)。 我的问题是我该怎么做?还是使用观察者更合适,例如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'),
对于变量名称选择不当等表示歉意......
我的模型:
学生
scores: DS.hasMany('score', {async: true}),
name: DS.attr('string')
目标
name: DS.attr('string'),
scores: DS.hasMany('score', {async : true})
得分
scoreResult: DS.attr('number'),
objective: DS.belongsTo('objective', {async: true}),
student: DS.belongsTo('student', {async: true})
-------------更新---------------
JSBin here。所以我想要返回其中一个分数,即控制台中记录的 1 或 2,作为计算属性“分数”。但我假设我只是返回一个未解决的承诺 - 对承诺没有经验 - 我如何适应才能解决?
--我也一直在想,对我在这里尝试做的事情采取不同的方法可能会更好。我可能会考虑创建一个具有适当“分数”模型的组件。如果可行,我会更新一些细节。
【问题讨论】:
-
你能把它扔进 ember.jsbin.com 吗?
-
应该是
model.scores.@each而不是model.@each.scores? -
已添加到 JSBin,但我已经破坏了它!当我开始输入夹具数据时停止工作。我一直在使用 Ember-CLI,我可以在 JSBin 中使用 DS.FixtureAdapter 吗?
-
可以,但是需要添加ember-data.js emberjs.jsbin.com/wafisa/1/edit?html,js,output
-
你有办法解决这个问题吗?
标签: ember.js ember-data promise