0

私のルート モデルでは、2 つのリクエスト (前と最新) を送信する必要があり、応答で ID を取得して他の 2 つのリクエスト (baslineCpature と currentcapture) を送信します。両方のリクエストの応答を受け取ったら、他の 2 つのリクエスト (fiberHealthData、wavelengthsPerSectionData) を送信する必要があります。ember モデルが返されます (baslineCpature、currentcapture、fiberHealthData、wavelengthsPerSectionData )。ここで私が抱えている問題の 1 つは、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+を読みましたか? これは、私が今まで読んだ中で最高の仕様の 1 つです。それを読んで!

あなたが間違っていたのは、の使用promiseA.baselineCaptureです。で解決しない限り、Promise にアクセスすることはできません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 に答える