簡體   English   中英

灰燼控制器屬性,承諾和范圍

[英]Ember Controller Property, Promise, and Scope

我對Javascript非常陌生,甚至對Ember都比較陌生,我有一個異步hasMany關聯,我試圖在父模型控制器中求和子模型(錦標賽)的屬性。 我對函數范圍的理解是內部函數將可以訪問包含函數中的所有內容(除此以外),到目前為止還可以。 我正在嘗試在諾言解決時增加金額,但是它必須使新的金額變量變大,因為一旦我將其返回到外部forEach,它又回到了0,而且我不確定我要去哪里錯誤。 我知道我沒有在.property()中設置依賴項,我只是想首先解決這個問題。

totalPrice: function(){
    var self = this;
    var allTourn = this.get('model.tournaments');
    var amount = 0;

    allTourn.forEach(function(tournament){
        var tID = Number(tournament.get('id'));

        self.store.find('tournament', tID).then(function(value){
            amount += Number(value.get('buyIn'));
            //amount is set to the buyIn property as expected
        })

        // amount is 0 out here... shouldnt the amount in the promise be using the outter amount variable?
    });

    return amount; // == 0
}.property()

基於托尼的答案的解決方案,我將其移至該路線,並在routes / package.js視圖中解決了承諾問題

export default Ember.Route.extend({
  model: function(params) {
    return this.store.find('package', params.package_id);
  },

  setupController: function(controller, model){
    var allTourn = model.get('tournaments');
    var self = this;

    var totalPricePromise = new Ember.RSVP.Promise(function(resolve, reject) {
      var tournyPromises = allTourn.map( function(tournament){
    var tID = Number(tournament.get('id'));
    return self.store.find('tournament', tID)
      });

      Ember.RSVP.all(tournyPromises).then(function(tournamentList){
    var amount = 0;
    tournamentList.forEach(function(tournament){
      amount += Number(tournament.get('buyIn'));
    })

    resolve(amount);
    controller.set('totalPrice', amount);
      });
    });
    controller.set('package', model);
  }

});

就像eggward在評論中說的那樣,totalPrice是在不等待store.find()承諾的情況下返回的金額。 在總價格完成后的某個時間,將承諾排隊等待稍后運行。

因此,如果必須等待promise進行計算,則可以從totalPrice返回promise,並在所有store.find()調用完成后對其進行解析。 您可以使用Ember promises的all()方法對發現進行分組。 例如,您可以嘗試如下操作,而不是allTourn.forEach:

// create a promise to return from totalPrice()
var totalPricePromise =  new Ember.RSVP.Promise(function(resolve, reject) {

  // create an array of promises from the find() calls.
  var tournyPromises = allTourn.map( function(tournament){
  var tID = Number(tournament.get('id'));
  return self.store.find('tournament', tID)
});

// user RSVP.all to wait for all the finds to complete
// it automatically groups all the results into an array
Ember.RSVP.all(tournyPromises).then(function(tournamentList){
  var amount = 0;
  tournamentList.forEach(tournament){
     amount += Number(tournament.get('buyIn'));
  })

  // this resolve is from the Ember.RSVP.Promise at the top
  // it will finally get to whoever called totalPrice()
  resolve(amount);
});

// return the promise from totalPrice, instead of the amount
return totalPricePromise;
});

但是,如果您例如在車把幫手中需要諾言,則諾言作為財產可能無法很好地發揮作用。 作為一種替代方法,您可以將所有的Promise東西移動到設置模型的路線上,以便在控制器中您已經擁有所有的錦標賽。 然后,您不必等待totalPrice函數中的find()操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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