【问题标题】:Adding a model to a Marionette CollectionView's collection doesn't trigger onItemAdd callback将模型添加到 Marionette CollectionView 的集合不会触发 onItemAdd 回调
【发布时间】:2013-04-23 22:20:03
【问题描述】:

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

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

【问题讨论】:

  • 只是好奇你为什么要添加空模型?
  • 在发布之前,我删除了该示例不需要的所有内容,并确保它仍然以同样的方式失败。

标签: marionette


【解决方案1】:

文档似乎已过时。您应该使用 onAfterItemAddedonBeforeItemAdded

这在 v1.0.0-rc2 中已更改

*中断:*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");   
    }
});

【讨论】:

  • 我也有同样的想法。看起来 Derek 已经更新了文档,但以某种方式破坏了它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-29
  • 2014-12-02
  • 1970-01-01
  • 2014-04-03
  • 2015-09-05
相关资源
最近更新 更多