嵌套承诺并在路由中返回它们不会更新路由模型和使用该模型的模板
Having nested promises and returning them in route won't update the route model and the template using that model
在我的路由模型中,我需要发送两个请求(上一个和最新的),并在响应中获取他们的 ID 以发送另外两个请求(baslineCpature 和 currentcapture)。当我收到两个请求的响应时,我需要发送另外两个请求(fiberHealthData、wavelengthsPerSectionData)。 ember 模型应该 return(baslineCpature、currentcapture、fiberHealthData、wavelengthsPerSectionData)。
我遇到的一个问题是,我想在收到对 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,
});
}
好的,首先这是你应该做的:
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 的特性,比如析构。它对这些用例非常有用!
您需要了解 promise 链的工作原理!你读过Promises/A+吗?这是我读过的最好的规范之一。读一读!
你用错了promiseA.baselineCapture
。如果不使用 then
.
解决它,您将无法访问承诺
这是我要做的工作:
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')}} )
});
});
});
});
在我的路由模型中,我需要发送两个请求(上一个和最新的),并在响应中获取他们的 ID 以发送另外两个请求(baslineCpature 和 currentcapture)。当我收到两个请求的响应时,我需要发送另外两个请求(fiberHealthData、wavelengthsPerSectionData)。 ember 模型应该 return(baslineCpature、currentcapture、fiberHealthData、wavelengthsPerSectionData)。 我遇到的一个问题是,我想在收到对 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,
});
}
好的,首先这是你应该做的:
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 的特性,比如析构。它对这些用例非常有用!
您需要了解 promise 链的工作原理!你读过Promises/A+吗?这是我读过的最好的规范之一。读一读!
你用错了promiseA.baselineCapture
。如果不使用 then
.
这是我要做的工作:
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')}} )
});
});
});
});