我想在 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/