我被困在試圖返回一堆承諾的值。Ember中的多個承諾不會在模板中解析
說明情況:有4個車型 - 學生 S IN一組有一個得分每個目標。
所以我做了一個組件,其中我通過了一個目標和組。該組件然後處理到商店的請求,以查找每個學生的得分,該目標,並返回平均值。
該代碼似乎工作,console.logs給我正是我想要的,直到最後,但承諾不會解析到模板中 - 我得到{對象對象},我相信是未解決的承諾。
我在這裏失蹤的東西?在等待多個承諾解決時是否需要額外的步驟?
我很欣賞這有點具體,當我明白答案時,我會嘗試重新解釋這個問題。
組件代碼:
averageScore: Ember.computed(function(){
var students = this.get('group.students');
var objective = this.get('objective');
var store = this.get('store');
var _this = this;
// Create an array of promises of each student's scores
var promises = [];
students.forEach((student) => {
var studentId = student.get('id');
var objectiveId = objective.get('id');
var newPromise = store.queryRecord('snapscore', { 'student' : studentId, 'objective': objectiveId });
promises.pushObject(newPromise);
});
// When promises resolve, find and return the average
return Ember.RSVP.allSettled(promises).then(function(scores){
let scoreTotal = 0;
let scoreCount = scores.length;
console.log("Score count is " + scoreCount);
scores.forEach((score)=>{
console.log("Student's score is " + score.value.get('score'));
scoreTotal = scoreTotal + score.value.get('score');
});
console.log("ScoreTotal is " + scoreTotal);
var average = scoreTotal/scoreCount;
console.log(average);
console.log(typeof(average));
return average;
});
}),
該組件的模板,然後簡單地返回averageScore。
謝謝
可以發誓我做之前......沒有吧!完美的答案,謝謝你的時間。使用didReceiveAttrs來設置屬性。 – rjoxford