【问题标题】:Backbone Marionette collection on DOM eventDOM 事件上的 Backbone Marionette 集合
【发布时间】:2014-05-16 13:37:50
【问题描述】:

我有一个 Backbone 集合和 Marionette CollectionView 呈现在一个无序列表中。我只是想让它可排序,所以我需要等到collectionView被放置在DOM中。

我可以监听在集合被渲染时触发的事件,但是当集合被放置在 DOM 中时我不知道如何做同样的事情。

var ContactView = Backbone.Marionette.CollectionView.extend({
    id : "contacts",
    tagName: "ul",
    itemView: UserView,
    initialize: function(){
        this.triggerMethod("collection:rendered", this);
    }
});

    var collection = new ContactView({ collection: myContacts });
    listenTo(collection, "collection:rendered",usersRendered);
    $("#content").append(collection.render().el);

    usersRendered: function(){
        $("#contacts").sortable(); // #contacts not in the DOM yet 
    }

Jsfiddle

【问题讨论】:

  • 您的问题是如何判断 CollectionView 何时已附加到 DOM?

标签: jquery-ui backbone.js marionette


【解决方案1】:

正如您所写,CollectionView 和 CompositeView 在渲染子视图后触发“collection:rendered”和“render”方法。 我能想到的唯一方法是在创建 ContactView 实例时提供 {el: '#contact'} 。或者,如果您要附加 view.el,那么您可以在执行此操作的地方调用 .sortable()。不过,第一种方法更好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-15
    • 1970-01-01
    • 2013-05-03
    • 2023-03-31
    • 1970-01-01
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多