Javascript EmberJS-在承诺解决后调用超级操作
我使用的是Ember 2.6.0 我正在扩展一个第三方组件,该组件在一个动作中定义了一些功能,我希望在我的子类中捕获该动作,调用一个返回承诺的函数,并在承诺解析时触发超级动作 因此,第三方组件执行以下操作:Javascript EmberJS-在承诺解决后调用超级操作,javascript,ember.js,ecmascript-6,ember-cli,Javascript,Ember.js,Ecmascript 6,Ember Cli,我使用的是Ember 2.6.0 我正在扩展一个第三方组件,该组件在一个动作中定义了一些功能,我希望在我的子类中捕获该动作,调用一个返回承诺的函数,并在承诺解析时触发超级动作 因此,第三方组件执行以下操作: import Ember from 'ember'; export default Ember.Component.extend({ actions: { theAction() { this._somePrivateFunction();
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);
})
}
}
});
this.\u super()
在回调中调用时不会解析为父组件操作。我尝试将supers函数存储为属性并调用它:
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);
})
}
这除了丑陋之外,还导致super操作中的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();
});
}
在子组件中,您喜欢:
theAction() {
const thePromise = this._funcThatReturnsPromise();
let parentAction = this._super;
let self = this;
thePromise.then(() => {
//parent usage
parentAction();
// this usage
self.doSome();
});
}
如果您使用的是真正的ES6
类
,超级。操作(…)
将开箱即用。@Bergi是的,这将是理想情况,但我在Emberjs组件的范围内工作:-(如果您使用的是真正的ES6类
,超级。操作(…)
将开箱即用。@Bergi是的,这将是理想的情况,但我在Emberjs组件的范围内工作:-(@Ebrahim Pasbani正如我在上面的问题中所说的那样,我已经尝试过,如果调用this,那么这个
在父操作中是未定义的way@TheMethod不,您尝试了其他操作。您确实将组件的属性设置为错误的。感谢您的回复。无论您是将this.\u super
分配给范围为的属性还是函数变量it仍然导致在then()中调用parentAction()
时未定义this
@Ebrahim Pasbani很抱歉,我的响应延迟了,我离开了一段时间。上述更改并不能解决我遇到的问题。问题是,在这样分配和调用时,未在父方法体中定义this
。父操作引用this
,以这种方式调用时,它是undefined
这是我找到的最好的解决方法。@Ebrahim Pasbani,正如我在上面的问题中所说,我已经尝试过,如果调用This,则在父操作中未定义This
way@TheMethod不,您尝试了其他操作。您确实将组件的属性设置为错误的。谢谢您的回复。是否this.\u super
对于属性或函数作用域变量,在then()中调用parentAction()
时,仍会导致this
未定义
@Ebrahim Pasbani很抱歉,我的响应延迟了,我离开了一段时间。上述更改并不能解决我遇到的问题。问题是,在这样分配和调用时,未在父方法体中定义this
。父操作引用this
,以这种方式调用时,它是未定义
这是我找到的最佳解决方法。