javascript - Ember.js 中具有相同事件的多个混合

标签 javascript ember.js mixins superclass super

我想在 Ember.js 的一个 View 中包含多个混入,并且多个混入和/或 View 使用相同的事件(例如 willInsertElement)。我正在运行 Ember 1.4.0-beta.5。

我知道每个 mixin 中的事件都会被 View 覆盖。但是,我读到可以在 mixin 和 View 中使用相同的事件 Hook ,或者在同一 View 中包含多个 mixin,方法是在mixin 的上述事件方法。但是,我未能成功实现这一目标。因此,我的问题是,如何在 View 和混合(或同一 View 中包含的多个混合)中的同一事件 Hook 内编写逻辑,以便调用事件 Hook 的每次出现中的所有逻辑。

这是一个例子:

App.StatsView = Em.View.extend(
    App.DateFormatting, {

    willInsertElement: function() {
        // Some view-specific logic I want to call here
    },
});

App.DateFormatting = Em.Mixin.create({

    willInsertElement: function() {
        this._super(); // This doesn't work.
        // Some mixin logic I want to call here
    },
});

注意这里的一种方法可能是不使用混合并改为扩展 View (因为 willInsertElement 特定于 Em.View),但这在我们的应用程序中是不可维护的。

最佳答案

如果您使用的不同函数彼此不依赖,最好的解决方案是不覆盖 willInsertElement Hook ,而是告诉函数在事件/ Hook 获取时引发打电话。

喜欢:

App.StatsView = Em.View.extend(App.DateFormatting, {
  someSpecificFunction: function () {
    console.log('beer me');
  }.on('willInsertElement')
});

App.DateFormatting = Em.Mixin.create({
  dateFormattingFunction: function () {
    console.log('beer you');
  }.on('willInsertElement')
});

关于javascript - Ember.js 中具有相同事件的多个混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22163263/

相关文章:

javascript - 为什么setTimeout循环只打印一次?

javascript - 如何防止在提交登录表单时清除我的密码?

javascript - 我无法让 Handlebars 助手使用meteorjs 中的参数

ember.js - 如何检测 EmberJS 中的一段时间不活动?

javascript - Ember.js 模板不迭代 {{#each}}

templates - 用于定义具有相同成员的结构的 Mixin 模板

c++ - 如何告诉 C++ mixin 它实现的接口(interface)

css - 媒体查询中的混合

javascript - Chrome——相当于 Firefox\IE 中的应用程序

javascript - 如何从 DOM 元素获取 Ext JS 组件