[英]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.