我試圖將<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',該怎麼辦?我打算最終添加。 –
更新的答案和一個附加示例。再一次,當你超越基本功能時,最好考慮自定義視圖。任何時候我想要在模板中解決複雜問題時,我都會將其視爲一種視圖,複雜的事情變得更容易。 –
好的,太棒了!非常感謝你! –