我试图将<tr>
的等级设置为#warning
或#success
,具体取决于我的服务器型号的percentFree属性。这是我的车把模板:按型号属性设置把手属性
<script type="text/x-handlebars" data-template-name="dashboard">
<h1>Virtual Image Overview</h1>
<table class="table table-hover">
<thead>
<tr>
<th>Machine Name</th>
<th>Drive</th>
<th>Total Size</th>
<th>Used Space</th>
<th>% free</th>
</tr>
</thead>
<tbody>
{{#each controller}}
<tr {{bindAttr class="status"}}>
<td>{{name}}</td>
<td>{{drive}}</td>
<td>{{totalSize}}</td>
<td>{{usedSpace}}</td>
<td>{{percentFree}}</td>
</tr>
{{/each}}
</tbody>
</table>
</script>
这是我的模型:
Dashboard.Server = DS.Model.extend({
name: DS.attr('string'),
drive: DS.attr('string'),
totalSize: DS.attr('number'),
usedSpace: DS.attr('number'),
percentFree: DS.attr('number'),
status: "",
setStatus: function() {
if(this.percentFree <= 0.50) {
this.status = "warning";
} else {
this.status = "success";
}
}
});
虽然类<tr>
是从来没有真正更新。有没有更有效的(正确的)方式去解决这个问题?
我自己也尝试status: this.setStatus()
和
setStatus: function() {
if(this.percentFree <= 0.50) {
return "warning";
} else {
return "success";
}
}
没有运气
如果我想添加第三个值,比如'#error',该怎么办?我打算最终添加。 –
更新的答案和一个附加示例。再一次,当你超越基本功能时,最好考虑自定义视图。任何时候我想要在模板中解决复杂问题时,我都会将其视为一种视图,复杂的事情变得更容易。 –
好的,太棒了!非常感谢你! –