本文介绍了将模型添加到Marionette CollectionView的集合不会触发onItemAdd回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我不确定我是否完全理解应该如何触发此回调.如果您采用准系统模型,请收集和查看以下信息:

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

并像这样实例化它们:

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

The PatchesView onItemAdded callback never triggers. Hmm...

推荐答案

文档似乎已过时.您应该使用 onAfterItemAdded onBeforeItemAdded

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

在v1.0.0-rc2中已更改

This was changed in v1.0.0-rc2

链接

这是摆弄您的示例的小提琴. http://jsfiddle.net/puleos/axJg4/

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

这篇关于将模型添加到Marionette CollectionView的集合不会触发onItemAdd回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 04:29