我正在尝试通过按如下所示插入this.send()
来在Ember中检测ActionHandler#send
:
Ember.ActionHandler.reopen({
send() { console.log("hooked"); this._super(...arguments); }
}
当我从
app.js
调用此应用程序时,它可以正常工作。当我从初始化程序调用它时,它没有。当我在应用程序启动后调用它(例如从应用程序 Controller 中)时,它也不起作用。在这两种都不起作用的情况下,如果我跟踪到this.send()
调用,它将直接进入send
的原始实现。我怀疑这与实例化对象时使用mixins的方式有关,但否则我很困惑。
最佳答案
使用初始化程序时,它可以正常工作:
initializers/action-hook.js
import Ember from 'ember';
export function initialize() {
Ember.ActionHandler.reopen({
send() {
console.log("hooked");
this._super(...arguments);
}
});
}
export default {
name: 'action-hook',
initialize: initialize
};
在应用程序 Controller 中测试。controllers/application.js
import Ember from 'ember';
export default Ember.Controller.extend({
afterInit: Ember.on('init', function() {
Ember.run.next(() => {
console.log('Send action.');
this.send('exampleAction');
});
}),
actions: {
exampleAction() {
console.log('exampleAction handled');
}
}
});
它输出:Send action.
hooked
exampleAction handled
Working demo和full code behind it。
关于ember.js - Ember : hooking into ActionHandler#send,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33416128/