Adding a model to the Marionette CollectionView does not call the onItemAdd callback

So, I'm not sure that I fully understand how this callback should work. If you take the barebones model, collection and views:

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"); } }); 

And create them like this:

 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); 

The PatchesView onItemAdded callback function never starts. Hm ...

+2
marionette
source share
1 answer

It looks like the documents are out of date. You must use onAfterItemAdded or onBeforeItemAdded

This has been changed in version 1.0.0-rc2

* BREAKING: * Changed the item:added event to before:item:added and after:item:added

Link

Here is the fiddle in which your example is reworked. http://jsfiddle.net/puleos/axJg4/

 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"); } }); 
+3
source share

All Articles