我有以下项目模型:是否可以设置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>
在这种情况下,我可能会使用三个模板或一个包含三个部分(顶部,中间,底部)的模板,并在数组之前将模板拆分为三个模板。 –
你是对的 - 保持疯狂的逻辑出模板。使用三个单独的模板 - 并在控制器中进行过滤,并让控制器将模板放在一个布局中。我如何让你的答案得分? –
我更新并复活了我的旧回答。 –