如果我为触发器和事件使用相同的事件配置设置Marionette视图,则只有触发器似乎被触发。以下是一个简化示例:点击.button
应运行show
并触发select:something
事件。但是,它只会触发事件。
var MyView = Marionette.CompositeView.extend({
ui: {
'button': '.button'
},
triggers: {
'click .button': 'select:something',
},
events: {
'click .button': 'show'
},
show: function() {
// won't fire
}
});
var view = new MyView({});
view.on('select:something', handleSelect); // will fire
Marionette trigger docs建议将stopPropogation
设置为false
会有所帮助,但这并没有改变我的行为:
triggers: {
'click .button': {
event: "select:something",
preventDefault: true,
stopPropagation: false
}
}, ...
答案 0 :(得分:1)
Marionette.View delegateEvents代码是:
// behavior events will be overriden by view events and or triggers
_.extend(combinedEvents, behaviorEvents, events, triggers, behaviorTriggers);
Backbone.View.prototype.delegateEvents.call(this, combinedEvents);
事件将被触发器覆盖,因为事件哈希是在_.extend中的触发器哈希之前。 您可以在事件中使用不同的键并触发哈希。像这样的代码:
var viewtemplate = _.template('<button class="button show-btn">show</button>');
var MyView = Marionette.CompositeView.extend({
template: viewtemplate,
triggers: {
'click .button': 'select:something',
},
events: {
'click .show-btn': 'show'
},
show: function() {
console.log('click .show-btn');
}
});