Ember:挂进ActionHandler #send

9 ember.js

我试图this.send()通过ActionHandler#send如下方式进入Ember :

Ember.ActionHandler.reopen({
  send() { console.log("hooked"); this._super(...arguments); }
}
Run Code Online (Sandbox Code Playgroud)

当我打电话给这个时app.js,当应用程序启动时,它可以工作.当我从初始化程序中调用它时,它没有.当我在应用程序启动后调用它时,例如来自应用程序控制器,它也不起作用.在它不起作用的两种情况下,如果我追踪到一个this.send()调用,它会直接进入原始实现send.

我怀疑这与在实例化对象时使用mixins的方式有关,但是否则我很难过.

Dan*_*mak 3

使用初始化程序时它确实有效:

初始化器/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
};
Run Code Online (Sandbox Code Playgroud)

在应用程序控制器中进行测试。

控制器/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');
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

它输出:

发送动作。

着迷的

example操作已处理

工作演示及其背后的完整代码