我找到了我的问题,但我想知道为什么,这样我可以(希望)更好地理解 Marionette/backbone。
问题下面的代码会在删除项目时更新视图:
var MainView = Marionette.ItemView.extend({
template: "#sample-template",
events :{
"click #remove" : "remove"
},
remove: function(){
this.trigger("property:remove", this.model);
}
});
var CollectionView = Marionette.CollectionView.extend({
itemView: MainView,
initialize: function(){
this.on("itemview:property:remove", function(view, model){
alert(this.collection.length);
this.collection.remove(model);
alert(this.collection.length);
});
}
});
修复下面的代码确实按预期更新了视图:
var MainView = Marionette.ItemView.extend({
template: "#sample-template",
triggers :{
"click #remove" : "property:remove"
},
});
var CollectionView = Marionette.CollectionView.extend({
itemView: MainView,
initialize: function(){
this.on("itemview:property:remove", function(view, model){
alert(this.collection.length);
this.collection.remove(view.model);
alert(this.collection.length);
});
}
});