3

所以,我不确定我是否完全理解这个回调应该如何被触发。如果您采用准系统模型、集合和视图:

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

并像这样实例化它们:

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 回调永远不会触发。唔...

4

1 回答 1

4

看起来文档已经过时了。您应该使用onAfterItemAddedonBeforeItemAdded

这在 v1.0.0-rc2 中已更改

* BREAKING: *item:added将事件更改为before:item:addedafter:item:added

关联

这是您的示例重做的小提琴。 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");   
    }
});
于 2013-04-24T15:18:51.073 回答