简体   繁体   English

EmberJS - 在承诺结算后调用超级行动

[英]EmberJS - Call super action after promise resolves

I am using Ember 2.6.0 我使用的是Ember 2.6.0

I am extending a third party component that has some functionality defined in a action and I want to catch that action in my subclass, call a function that returns a promise and fire the supers action when the promise resolves. 我正在扩展第三方组件,该组件具有在操作中定义的某些功能,并且我想在我的子类中捕获该操作,调用返回promise的函数并在promise解析时触发超级操作。

So the third party component does this: 所以第三方组件这样做:

 import Ember from 'ember';

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

And in my subclass I am doing: 在我的子类中,我正在做:

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);
        })
     }
  }
});

this._super() does not resolve to the parent component action when called in the promises callback. 在promises回调中调用时, this._super()不会解析为父组件操作。 I've tried storing the supers function as a property and calling it: 我已经尝试将超级函数存储为属性并调用它:

   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, in addition to being ugly, results in this inside the supers action to be undefined. 除了丑陋之外, this导致超级动作内部未定义。 I'm not sure why that is.. looking through some docs. 我不确定为什么会这样......通过一些文档查看。

There is also the option of calling send() in my subclasses action: 还有在子类操作中调用send()的选项:

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

But this of course results in an infinite loop since the function ends up calling itself. 但这当然导致无限循环,因为函数最终调用自身。

I'm not sure how to proceed here. 我不知道该怎么办。 Could anyone tell me if there is a clean way to do what I am trying to do here? 谁能告诉我是否有一种干净的方式来做我想做的事情? Any advice would be appreciated. 任何意见,将不胜感激。 Thanks much! 非常感谢!

In child component do like : 在子组件中做:

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