将模型添加到Marionette CollectionView的集合不会触发onItemAdd回调

时间:2013-04-23 22:20:04

标签: marionette

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

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

1 个答案:

答案 0 :(得分:3)

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

在v1.0.0-rc2

中已更改
  

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

Link

这是你的例子重新设计的小提琴。 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");   
    }
});