Adding a model to a Marionette CollectionView's collection doesn't trigger onItemAdd callback
所以,我不确定我是否完全理解这个回调应该如何被触发。如果您采用准系统模型、集合和视图:
1 2 3 4 5 6 7 8 9 10 11 12 | PatchModel = Backbone.Model.extend({}); PatchCollection = Backbone.Collection.extend({model: PatchModel}); PatchView = Backbone.Marionette.ItemView.extend({template:'#patchview'}); PatchCollectionView = Backbone.Marionette.CollectionView.extend({ itemView:PatchView ,onItemAdded: function(itemView){ console.log("item was added"); } }); |
然后像这样实例化它们:
1 2 3 4 5 6 7 8 9 | Patch0 = new PatchModel({}); Patch1 = new PatchModel({}); Patches = new PatchCollection(); PatchesView = new PatchCollectionView({collection:Patches,el:"dom_id"}); Patches.add(Patch0); PatchesView.render(); Patches.add(Patch1); |
PatchesView onItemAdded 回调永远不会触发。嗯……
文档似乎已过时。您应该使用 onAfterItemAdded 或 onBeforeItemAdded
这在 v1.0.0-rc2 中已更改
*BREAKING: * Changed the
item:added event tobefore:item:added andafter:item:added
链接
这是对您的示例进行了修改的小提琴。
http://jsfiddle.net/puleos/axJg4/
1 2 3 4 5 6 7 8 9 10 11 12 | var PatchCollectionView = Backbone.Marionette.CollectionView.extend({ itemView: PatchView, initialize: function() { _.bindAll(this); }, onAfterItemAdded: function(itemView){ console.log("item was added"); }, onRender: function(){ console.log("render"); } }); |