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