ember.js - Ember : hooking into ActionHandler#send

标签 ember.js

我正在尝试通过按如下所示插入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 demofull code behind it

关于ember.js - Ember : hooking into ActionHandler#send,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33416128/

相关文章:

ember.js - 在输入帮助器中绑定(bind)数据属性

javascript - 无法在 ember 中向后端服务器发出请求(错误 :'No Access-Control-Allow-Origin' )

javascript - Qunit 失败并出现 TypeError : $ is not a function

ember.js - 当应用程序位于我的服务器的子目录中时,如何设置 ember.js 以使用历史记录

jquery - 如何将 JQuery 可排序/拖放与模型同步?

javascript - Ember 和 Require.JS 优化器 "Error: Could not find module jquery"

ember.js - 当 EmberJS 中的模型属性更改时重新渲染 View

ember-cli 配置/环境中的 Javascript 对象

ember.js - 在 Jade 中使用 Ember.js 和 Handlebars.js View

javascript - Ember.js:对模型进行分组(使用 ArrayProxy)