我有以下項目模型:是否可以設置handlebars.js/moustache.js模板的不同部分?
{
the_yearId: "2009",
title: "First 2009 Project",
className: "header",
id: "null",
images: [
{
title: "first-2009-project-image1.png",
caption: "about first 2009 project image 1"
},
{
title: "second-2009-project-image2.png",
caption: "about first 2009 project image 2"
}
]
}
{
the_yearId: "2009",
title: "Second 2009 Project",
className: "leftColumn",
id: "null",
images: [
{
title: "second-2009-project-image1.png",
caption: "about second 2009 project image 1"
},
{
title: "second-2009-project-image2.png",
caption: "about second 2009 project image 2"
}
]
}
{
the_yearId: "2009",
title: "Third 2009 Project",
className: "rightColumn",
id: "null",
images: [
{
title: "third-2009-project-image1.png",
caption: "about third 2009 project image 1"
},
{
title: "third-2009-project-image2.png",
caption: "about third 2009 project image 2"
}
]
}
{
the_yearId: "2010",
title: "First 2010 Project",
images: [
{
title: "first-2010-project-image1.png",
caption: "about first 2010 project image 1"
},
{
title: "second-2010-project-image2.png",
caption: "about first 2010 project image 2"
}
]
}
重複此模式爲其他年直至2012年。當客戶點擊了一年,與之搭配的the_yearId
模型放入Backbone.Collection,這是然後遞交給視圖,以及適當的模板。非常直截了當。
我使用handlebars.js爲模板,車把代碼副本如下:
{{title}}
{{#each images}}
{{this.title}}
{{this.caption}}
{{/each}}
這將通過集合中的每個模型可預測環路和正確填充。如果我在每次迭代中添加HTML結構和CSS樣式,則樣式將相同。
有沒有辦法以不同方式對頁面的每個部分進行樣式設置?例如,如果集合包含4個模型,我如何設計把手模板的樣式以將第一個模型放在100%寬度的標題中,第二個和第三個模型放在另一個部分中,每個模型的樣式都是50%寬度。第四個模型將被設計爲100%寬度的頁腳。
將jQuery的動態分配給模型一旦他們被放置在DOM中會有用嗎?有沒有更好的辦法?
編輯 我想做的事:
<!-- if model.className == "header" -->
<section id="single-column">
<article>
<header>First 2009 Project</header>
<img src="first-2009-project-image1.png"></img>
</article>
</section>
<section id="two-column">
<!-- if model.className == "leftColumn" -->
<article>
<header>Second 2009 Project</header>
<img src="second-2009-project-image1.png">
</article>
<!-- if model.className == "rightColumn" -->
<article>
<header>Third 2009 Project</header>
<img src="third-2009-project-image1.png">
</article>
</section>
<footer>
<!-- if model.className == "footer" -->
<header>Fourth 2009 Project</header>
<img src="fourth-2009-project-image1.png">
</footer>
在這種情況下,我可能會使用三個模板或一個包含三個部分(頂部,中間,底部)的模板,並在數組之前將模板拆分爲三個模板。 –
你是對的 - 保持瘋狂的邏輯出模板。使用三個單獨的模板 - 並在控制器中進行過濾,並讓控制器將模板放在一個佈局中。我如何讓你的答案得分? –
我更新並復活了我的舊回答。 –