1
我在学习Backbone.Marionette,我需要了解如何使用LayoutView在模板中显示模型。如何使用LayoutView显示集合?
在此基础上例如:
App.js:
//VIEWS
ArticleView = Backbone.Marionette.ItemView.extend({
template: "#articleTemplate",
tagName: 'li',
});
SelectedView = Backbone.Marionette.ItemView.extend({
template: "#selectedTemplate",
tagName: 'li',
});
var AppLayoutView = Backbone.Marionette.LayoutView.extend({
template: "#layout-view-template",
regions: {
articles: "#articles",
selecteds: "#selecteds"
}
});
myApp.addInitializer(function(options) {
var layoutView = new AppLayoutView();
layoutView.render();
layoutView.articles.show(new ArticleView(options.articles));
layoutView.selecteds.show(new SelectedView(options.selecteds));
});
nsApp.start({
articles: Articlescollection,
selecteds: SelectedsCollection
});
的index.html:
布局模板和模板ItemViews:
<script id="layout-view-template" type="text/template">
<section>
<article id="articles">
/* show My first collection here */
</article>
<article id="selecteds">
/* show My second collection here */
</article>
</section>
</script>
<script type="text/template" id="articleTemplate">
<img src="<%= thumbnail %>" alt="<%= title %>" class="img-thumbnail"/>
<%= title %>
<hr>
</script>
<script type="text/template" id="selectedTemplate">
<img src="<%= thumbnail %>" alt="<%= title %>" class="img-thumbnail"/>
<%= title %>
<hr>
</script>
我真的很困惑这里!
谢谢Vahan这是非常有帮助的。现在我正在使用ItemView&CollectionView和LayoutView。 LayoutView背后的动机是处理collectionViews区域外部存在的一些按钮事件。因此,我认为CompositeView可能更适合这种用途。 – elhoucine 2014-09-22 12:53:30
'CompositeView'绝对是你想要的,而不是所有这些自定义代码。 'CompositeView'基本上是'LayoutView'与'CollectionView'合并的。 – 2014-09-26 13:23:59
@HeinHaraldsonBerg你总是欢迎你添加没有所有这个自定义代码的答案。 – 2016-12-21 13:16:27