簡體   English   中英

僅在EmberJS中實現多次承諾后執行代碼

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM