2017-07-27 35 views
2

我被困在試圖返回一堆承諾的值。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

謝謝

回答

1

計算屬性不是承諾意識。因此,不要在計算屬性內部返回Promise,並且通常不會在計算屬性內執行任何設置。

在你的情況,你可以有屬性命名averageScore和做任何的組件生命週期掛鉤方法編寫代碼。(可能是initdidReceiveAttrs鉤)。您可以將結果設置爲averageScore屬性this.set('averageScore',result)

如果你仍然想使計算性能答應意識到然後按照這個ember igniter link

+1

可以發誓我做之前......沒有吧!完美的答案,謝謝你的時間。使用didReceiveAttrs來設置屬性。 – rjoxford