【发布时间】:2016-06-02 11:49:01
【问题描述】:
我正在使用 Marionette 2.4 并且有一个 layoutView 正在监听 childView 中的事件。当事件触发时,我在集合中搜索现有模型,如果不存在,我创建一个新模型并将其添加到集合中。如果找到它,我会从集合中删除该模型。问题是该事件似乎被触发了两次。第一次触发时,它会创建模型,但是当它触发两次时,它会在集合中找到新创建的模型,然后将其删除。
var layout = Marionette.LayoutView.extend({
childEvents: {
'channel:selected': 'onChildviewChannelSelected'
},
onChildviewChannelSelected: function (childView, args) {
var linkCollection = this.getRegion('regionWithCollectionView').currentView.collection;
var modelToUpdate = linkCollection.where({channel: args.currentTarget.value});
if(modelToUpdate) {
this.removeModel(linkCollection, modelToUpdate);
} else {
this.addModel(linkCollection, args.currentTarget.value);
}
},
removeModel: function (collection, model) {
collection.remove(model);
},
addModel: function (collection, channel) {
var newEntity = new MyApp.Entities.Link();
newEntity.set('channel', channel);
collection.add(newEntity);
}
});
这里是触发 'channel:selected' 事件的子视图......
var childView = Marionette.ItemView.extend({
events: {
'change input[type="checkbox"]': 'channelSelected'
},
channelSelected: function(args) {
this.triggerMethod('channel:selected', args);
}
});
知道为什么 childView 会触发 'channel:selected' 事件两次吗?
保存要添加到的集合的不是视图,但是当将集合添加到其中时可能会发生一些事情,它会由于某种原因再次触发事件。
【问题讨论】:
标签: backbone.js collections marionette