1

I need to render only first element of fetched collection.

ItemView = Backbone.Marionette.ItemView.extend({
    template:  "#item"
});

ItemsEmptyView = Backbone.Marionette.ItemView.extend({
    template: "#items_empty"
});

ItemsView = Backbone.Marionette.CompositeView.extend({
    template: "#items",
    itemView: ItemView,
    emptyView: ItemEmptyView
});

How can I accomplish that? I've tried to override render() method, or onBeforeRender() nothing helps.

4

2 回答 2

3

If you really need to do this, use appendHtml().

appendHtml: function(collectionView, itemView, index){
    if ( index === 0 )
        collectionView.$el.append(itemView.el);
}

https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.compositeview.md#compositeviews-appendhtml

于 2013-09-13T14:42:31.957 回答
1

最简单的方法是创建一个仅包含第一个模型的新集合实例,并将其传递给复合视图。当然,这是假设您不能简单地使用项目视图。

于 2013-09-13T14:31:02.490 回答