[英]Execute Code only after Multiple Promises Fulfilled in EmberJS
我有一個EmberJS ArrayController。 我想在這個控制器上有一個計算屬性, neurons
,它是model
屬性的一個子集。 子集是基於側欄中的切換按鈕計算的,該切換按鈕綁定到currentDataset
。 另一個計算屬性, activePlots
則依賴於neurons
; 該Neuron
模型具有hasMany
關系Plot
,並activePlots
加載所有的情節對象在每個神經元的對象關聯neurons
。
目前我正在嘗試使用mapBy
,但我mapBy
了一個問題。 每次檢索Neuron
對象的plots
返回PromiseArray
。 我需要立即操縱所有返回的圖。 我明白我可以打電話then
對單個呼叫的承諾結果get('plots')
但我怎么只有后執行代碼get('plots')
調用返回的所有神經元?
neurons: ( ->
@get('model').filterBy('dataset', @get('currentDataset'))
).property('model', 'currentDataset'),
activePlots: ( ->
plots = @get('neurons').mapBy('plots')
# ...code to execute after all plots have loaded
).property('neurons')
更新:從控制台輸出圖片console.log(plotSets)
內then
回調
Ember.RSVP.all(@get('neurons').mapBy('plots')).then (plotSets) ->
console.log(plotSets)
有一種方便的方法來組合promises: Ember.RSVP.all(ary)
接受一個promises數組,並成為一個promise,當輸入數組中的所有promise都被解析時,它將被解析。 如果拒絕一個,則拒絕all()
保證。
這非常方便,例如,當啟動多個並行網絡請求時,並在完成所有這些請求時繼續。
除了Steve說你可以觀看nuerons.length並使用Ember.scheduleOnce來安排更新(下面猜到了coffeescript)
activePlots: [],
watchNuerons: ( ->
Ember.run.scheduleOnce('afterRender', this, @updatePlots);
).observes('nueron.length'),
updatePlots: ( ->
plots = @get('neurons').mapBy('plots')
# ...code to execute after all plots have loaded
@set('activePlots', plots)
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.