我有一个小胡子文件,我正在迭代数组:
var data = {
sales: [
{
name: "Jim Frost",
region: "USA East",
phone: "212-555-1212",
email: "jfrost@acme-travel.com"
},
{
name: "Jan Smith",
region: "USA West",
phone: "310-555-1212",
},
{
name: "Fred Wesley",
phone: "608-555-1212",
email: "fwesley@acme-travel.com"
},
{
name: "Lisa Moore",
region: "USA South",
phone: "315-555-1212",
email: "lmoore@acme-travel.com"
},
{
name: "Jim Dio",
phone: "+ 44 022-555-1212",
email: "jdio@acme-travel.com"
},
{
name: "Charles Watts",
region: "Spain",
email: "cwatts@acme-travel.com"
},
{
name: "Bert Cooper",
region: "Italy",
email: "bcooper@acme-travel.com"
}
]
};
这是标记:
<div>
<section>
{{#data.sales}}
<article class="items">
<div class="region">{{{region}}}</div>
</article>
{{/data.sales}}
</section>
</div>
如果区域为USA East
,我想添加一些特殊样式(如粗体字体,颜色等)。
如果{{{region}}
具有特定值,我如何在article元素中检测这个固有循环?鉴于将根据我从后端获得的值进行比较,请说{{myValue}},后者在后端手动设置为 USA East 。
答案 0 :(得分:1)
您可以在data
中添加一个函数,该函数将根据区域值返回正确的类。像
data['regionClass'] = function(){
if ( this['region'] == 'USA East' ) {
return "strong green";
}else{
return "";
}
}
然后在Mustache中你可以做:<div class="region {{regionClass}}">{{{region}}}</div>