Adding a model to a Marionette CollectionView's collection doesn't trigger onItemAdd callback

人盡茶涼 提交于 2019-12-10 19:54:43

问题


So, I'm not sure I quite understand how this callback is supposed to be triggered. If you take a 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 instantiate 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 never triggers. Hmm...


回答1:


It looks like the docs are out of date. You should use onAfterItemAdded or onBeforeItemAdded

This was changed in v1.0.0-rc2

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

Link

Here is a fiddle with your example 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");   
    }
});


来源:https://stackoverflow.com/questions/16180159/adding-a-model-to-a-marionette-collectionviews-collection-doesnt-trigger-onite

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!