0

在我的路由模型中,我需要发送两个请求(以前的和最新的),并在响应中获取他们的 ID 以发送另外两个请求(baslineCpature 和 currentcapture)。当我得到两个请求的响应时,我需要发送另外两个请求(fiberHealthData、wavelengthsPerSectionData)。ember 模型应该返回(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,
  });
}

4

2 回答 2

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.

于 2016-05-31T18:36:40.930 回答
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')}} )
              });

            });
          });
        });

于 2016-05-31T19:20:01.083 回答