【问题标题】:Initialize CollectionView with a collection reference使用集合引用初始化 CollectionView
【发布时间】:2013-06-24 22:41:06
【问题描述】:

在我的 CollectionView Backbone Marionette 对象的初始化函数中,我正在执行以下操作:

this.collection_addresses = new AddressCollectionView({
    el: 'ul.addresses',
    collection: options.user.get("addresses")
});

但是,每当options.user.get("addresses") 中引用的对象发生更改时,AddressCollectionView 永远不会更新,我认为Marionnette 会自动处理这个问题。使用 fetch 更新用户对象。有什么想法吗?

编辑#1 只是为了澄清集合视图是这样的

var AddressCollectionView = Backbone.Marionette.CollectionView.extend({
    itemView: AddressItemView,
    tagName: 'ul'
});

谢谢。

【问题讨论】:

  • 从文档看来您仍然需要“收听”收集事件
  • @AstDerek 为什么? collectionview 初始化器通常会监听 add/remove/reset
  • 您没有添加/删除/重置集合。您正在修改该集合上的模型。

标签: backbone.js marionette


【解决方案1】:

来自 v1.0.3 源代码,这里是 collectionview 监听的事件

_initialEvents: function(){
    if (this.collection){
      this.listenTo(this.collection, "add", this.addChildView, this);
      this.listenTo(this.collection, "remove", this.removeItemView, this);
      this.listenTo(this.collection, "reset", this.render, this);
    }
  },

collectionView 接受 itemView 视图对象定义(不是实例)。此 itemView 由 collectionview 使用集合中每个项目的模型初始化。我会扩展木偶的 itemview 类,并在那里监听模型上的更改事件,并将 itemview 视图对象定义(不是实例)注入到 collectionview 中。

【讨论】:

  • 你的 itemView 需要在它的初始化块中监听模型改变事件,然后你就应该完成了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-01
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多