EmberJS - 在承诺解决后调用超级操作
EmberJS - Call super action after promise resolves
我正在使用Ember 2.6.0
我正在扩展一个第三方组件,该组件在操作中定义了某些功能,我想在我的子类中捕获该操作,调用返回 promise 的函数并在 promise 解析时触发 supers 操作。
所以第三方组件这样做:
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._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);
})
}
这除了丑陋之外,还导致超动作内部的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();
});
}
相关文章:
- 无法在mvc视图中使用ajax调用获取操作返回的模型对象列表
- 是否可以将alertify.js的cancel按钮和onclose调用的操作分开
- Rails:如何在jQuery(.js.erb)中调用“create”操作
- 如果条件,则在其他操作之后调用 Redux 操作
- 有没有办法从 javascript 函数调用控制器操作
- 如何返回一页并检查调用该操作的页面
- 在视图中未使用Javascript调用控制器操作
- 父操作可防止在 ember.js 中调用提交操作
- 如何在构造函数之外调用超级构造函数
- 使用 ajax 调用使用操作类返回的数据刷新(更新)表的各个行
- 如何根据组合框调用控制器操作
- 从 javascript 同步调用 Struts 2 操作
- ajax 调用控制器操作,将对象作为参数
- JavaScript ES6 承诺在 while 循环中调用异步操作
- Android WebView 应该覆盖 UrlLoad,我应该调用超级方法还是只返回 false
- 调用 Struts2 操作,每个 jQuery DataTable 行中的参数
- MVC - 在 ajax 调用中操作 Jquery 插件属性
- 视图中的按钮,用于调用控制器操作以更新数据库列
- 如何将参数传递给在 JQuery dialog() Open 事件上调用的操作
- EmberJS - 在承诺解决后调用超级操作