2016-05-31 24 views
0

在我的路線模型我需要發送兩個請求(以前和最新),並在響應我抓住他們的ID發送兩個其他請求(baslineCpature和currentcapture)。當我得到這兩個請求的響應時,我需要發送兩個其他請求(fiberHealthData,wavelengthPerSectionData)。應該返回的模型應該是(baslineCpature,currentcapture,fiberHealthData,wavelengthPerSectionData)。 我在這裏有一個問題是,我想盡快更新我的模板,我得到了baslineCpature和currentcapture的響應。嵌套承諾,並返回他們的路線將不會更新路線模型和模板使用該模型

這是我的代碼。我很感激,如果有人能告訴我我做錯了什麼。

model: function (params,transition) { 
 

 
    var promiseA=null; 
 
    var promiseB=null; 
 
    var promiseA1=null; 
 
    var promiseB1=null; 
 

 
    promiseA1 = Ember.RSVP.hash({ 
 
    latest:this.store.findAll('latest'), 
 
    previous: this.store.findAll('previous'), 
 
    }); 
 

 
    promiseA= promiseA1.then((promiseAResolved) => { 
 
    return Ember.RSVP.hash({ 
 
     baselineCapture:self.store.find('capture', promiseAResolved.previous.get('firstObject.id')), 
 
     currentCapture: self.store.find('capture', promiseAResolved.latest.get('firstObject.id')), 
 
    }); 
 
    }); 
 

 
    promiseB= promiseA.then((promiseAResolved) => { 
 
    baselineId = promiseAResolved.baselineCapture.id; 
 
    currentId = promiseAResolved.currentCapture.id; 
 

 
    return Ember.RSVP.hash({ 
 
     fiberHealthData:self.store.findAll('fiber-health',{ adapterOptions: {current: currentId, baseline: baselineId}}), 
 
     wavelengthsPerSectionData:self.store.findAll('wavelengths-per-section',{ adapterOptions: {current: currentId, baseline: baselineId}}) 
 
    }); 
 
    }); 
 

 
//this should be retun of my model 
 
    return Ember.RSVP.hash({ 
 
    baselineCapture:promiseA.baselineCapture, 
 
    currentCapture:promiseA.currentCapture, 
 
    fiberHealthData:promiseB.fiberHealthData, 
 
    wavelengthsPerSectionData:promiseB.wavelengthsPerSectionData, 
 
    }); 
 
}

+1

您正在使用promiseA'和'promiseB'的'結果他們在你的最終散列已經解決了。 – Kingpin2k

回答

1

好吧,首先,這是你應該做的:

const {hash} = Ember.RSVP; 

return hash({ 
    latest:this.store.findAll('latest'), 
    previous: this.store.findAll('previous'), 
}).then(({latest, previous}) => hash({ 
    baselineCapture:self.store.find('capture', previous.get('firstObject.id')), 
    currentCapture: self.store.find('capture', latest.get('firstObject.id')), 
})).then(({baselineCapture, currentCapture}) => { 
    let current = currentCapture.id; 
    let baseline = baselineCapture.id; 
    return hash({ 
     currentCapture, 
     baselineCapture, 
     fiberHealthData:self.store.findAll('fiber-health',{ 
      adapterOptions: {current, baseline} 
     }), 
     wavelengthsPerSectionData:self.store.findAll('wavelengths-per-section', { 
      adapterOptions: {current, baseline} 
     }), 
    }); 
}); 

使用ES6功能,如自毀。它對這些用例非常有用!

您需要了解承諾鏈如何工作!你讀過Promises/A+嗎?這是我讀過的最好的規範之一。閱讀!

你做錯了什麼是使用promiseA.baselineCapture。如果沒有用then解決該問題,您將無法訪問該承諾。

+0

讓我們來看看你的答案是否有效,如果你非常渴望downvote而不是,return語句不會丟失。 –

0

這裏是我開始工作:

 return new Ember.RSVP.Promise(resolve => { 
 
      Ember.RSVP.hash({ 
 
      latest: this.store.findAll('latest'), 
 
      previous: this.store.findAll('previous'), 
 
      }).then((promiseA1Resolved) => { 
 
      Ember.RSVP.hash({ 
 
       baselineCapture:self.store.find('capture', promiseA1Resolved.previous.get('firstObject.id')), 
 
       currentCapture: self.store.find('capture', promiseA1Resolved.latest.get('firstObject.id')), 
 
      }).then((promiseAResolved) => { 
 
       self.updateContext(promiseAResolved,self); 
 
       resolve({ 
 
       baselineCapture: promiseAResolved.baselineCapture, 
 
       currentCapture: promiseAResolved.currentCapture, 
 
       fiberHealthData:self.store.findAll('fiber-health',{ adapterOptions: {current: self.get('contextService._currentId'), baseline:self.get('contextService._baselineId') }}), 
 
       wavelengthsPerSectionData:self.store.findAll('wavelengths-per-section',{ adapterOptions: {current: self.get('contextService._currentId'), baseline: self.get('contextService._baselineId')}}) 
 
       }); 
 

 
      }); 
 
      }); 
 
     });