在Ember.js中具有相同事件的多个Mixins

Dun*_*ker 1 javascript super mixins superclass ember.js

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

我知道每个mixin中的事件都会被视图覆盖.但是,我已经读过,通过this._super();在mixin的上述事件方法的开头调用,可以在mixin和view中使用相同的事件挂钩,或者在同一视图中包含多个mixin.但是,我无法成功实现这一目标.因此,我的问题是,如何在视图和mixin(或同一视图中包含的多个mixin)中的同一事件挂钩中编写逻辑,以便调用每次出现的事件挂钩中的所有逻辑.

这是一个例子:

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
    },
});
Run Code Online (Sandbox Code Playgroud)

NB这里的一种方法可能是不使用mixin并扩展视图(因为willInsertElement特定于Em.View),但在我们的应用程序中无法维护.

her*_*rom 5

如果你正在使用的不同函数彼此不相关,那么它是不覆盖willInsertElement钩子的最佳解决方案,而是在调用事件/钩子时告诉函数被引发.

喜欢:

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