簡體   English   中英

EmberJS - 在承諾結算后調用超級行動

[英]EmberJS - Call super action after promise resolves

我使用的是Ember 2.6.0

我正在擴展第三方組件,該組件具有在操作中定義的某些功能,並且我想在我的子類中捕獲該操作,調用返回promise的函數並在promise解析時觸發超級操作。

所以第三方組件這樣做:

 import Ember from 'ember';

export default Ember.Component.extend({
  actions: {
     theAction() {
         this._somePrivateFunction();
        //do other stuff
     }
  }
});

在我的子類中,我正在做:

import Ember from 'ember';
import ThirdPartyComponent from 'path/to/component'

export default ThirdPartyComponent.extend({
  _funcThatReturnsPromise() {
     return new Ember.RSVP.Promise();
  }
  actions: {
     theAction() {
        const thePromise = this._funcThatReturnsPromise();
        thePromise.then(() => {
            // undefined!
            this._super(...arguments);
        })
     }
  }
});

在promises回調中調用時, this._super()不會解析為父組件操作。 我已經嘗試將超級函數存儲為屬性並調用它:

   theAction() {
            const thePromise = this._funcThatReturnsPromise();
            this.set('superFunc', this._super);
            thePromise.then(() => {
           // calls the super but "this" inside the supers action is undefined
           this._super(...arguments);
       })
   }

除了丑陋之外, this導致超級動作內部未定義。 我不確定為什么會這樣......通過一些文檔查看。

還有在子類操作中調用send()的選項:

   theAction() {
      const thePromise = this._funcThatReturnsPromise();
      this.set('superFunc', this._super);
      thePromise.then(() => {
          //infinite loop!
          this.send('theAction');
      });
   }

但這當然導致無限循環,因為函數最終調用自身。

我不知道該怎么辦。 誰能告訴我是否有一種干凈的方式來做我想做的事情? 任何意見,將不勝感激。 非常感謝!

在子組件中做:

theAction() {
      const thePromise = this._funcThatReturnsPromise();
      let parentAction = this._super;
      let self = this;
      thePromise.then(() => {
          //parent usage
          parentAction();

          // this usage
          self.doSome();
      });
   }

暫無
暫無

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

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