嵌套视图应该是布局视图吗?(Should nested views be layout views?)

一个简单而简短的问题:如果视图包含两个或更多子视图。 视图容器应该是布局视图吗?

如果没有,有什么好的选择?

更新:

我的代码:

var LikeButtonModal = Backbone.Model.extend({ url: 'api/profile/like/' }); var LikeButton = Backbone.Marionette.ItemView.extend({ tagName: 'button', className: 'like', template: '<div>like</div>', events: { 'click' : 'like' }, initialize: function(userId){ this.model = new LikeButtonModal(); }, like: function(){ this.model.save(); } }) var LeftProfileView = Backbone.Marionette.Layout.extend({ template: '#profile-left', regions:{ extra : '.extra' }, initialize: function(){ this.on("item:rendered", this.editable, this); }, onRender: function(){ if(this.model.get('userid') != ActiveUser.get('userid')){ this.extra.show(new LikeButton(this.model.get('userid'))); } } });

A simple and short question: If a view contains two or more sub views. Should the view container be a layout view?

If not, what are good alternatives?

Update:

my code:

var LikeButtonModal = Backbone.Model.extend({ url: 'api/profile/like/' }); var LikeButton = Backbone.Marionette.ItemView.extend({ tagName: 'button', className: 'like', template: '<div>like</div>', events: { 'click' : 'like' }, initialize: function(userId){ this.model = new LikeButtonModal(); }, like: function(){ this.model.save(); } }) var LeftProfileView = Backbone.Marionette.Layout.extend({ template: '#profile-left', regions:{ extra : '.extra' }, initialize: function(){ this.on("item:rendered", this.editable, this); }, onRender: function(){ if(this.model.get('userid') != ActiveUser.get('userid')){ this.extra.show(new LikeButton(this.model.get('userid'))); } } });

最满意答案

如果您要在不同时间替换子视图,或者如果子视图的类型非常不同,则布局对此有好处...例如,布局可能包含您的标题,导航和主要内容区域。

其他选项包括CollectionViews和CompositeViews。

集合视图将呈现项目集合,对集合中的每个项目使用相同类型的视图。 这适用于事物列表。

CompositeViews是CollectionViews,可以围绕集合呈现包装器模板。 例如,HTML表结构。 table , thead , tbody和tfooter标签可以在CompositeView的包装器模板中呈现,然后可以将项集合​​呈现到tbody标签中。

这也可能会对这个问题有所了解: https : //github.com/derickbailey/backbone.marionette/wiki/Use-cases-for-the-different-views

Layouts are good for this if you will be replacing the sub-views at different times, or if the sub-views are very different types... for example, a layout might contain your header, your navigation and your main content region.

Other options are CollectionViews and CompositeViews.

Collection views will render a collection of items, using the same type of view for each item in your collection. This works well for lists of things.

CompositeViews are CollectionViews that can render a wrapper template around the collection. For example, an HTML table structure. The table, thead, tbody and tfooter tags can be rendered in the CompositeView's wrapper template, and then a collection of items can be rendered in to the tbody tag.

This might shed a little more light on the subject, too: https://github.com/derickbailey/backbone.marionette/wiki/Use-cases-for-the-different-views

更多推荐