[英]In Meteor how to pass entire object to template helper?
我目前有一个模板帮助器,用于根据某些逻辑设置表行的颜色。 帮助程序需要几个字段来确定。
<template name="files">
.
.
<thead>...</thead>
<tbody>
{{#each files}}
<tr class="status" style="background-color:{{getStatusColor deleted_on changed_on created_on}}">
<td>{{_id}}</td>
<td>{{created_on}}</td>
<td>{{changed_on}}</td>
<td>{{deleted_on}}</td>
</tr>
{{/each}}
</tbody>
和(缩写)JS:
Template.files.helpers({
/* returns background color for row based on file properties */
getStatusColor: function(deleted_on, changed_on, created_on) {
if(deleted_on)
return foo...
if(changed_on)
return bar...
}
});
这很好用,但是我似乎需要每个属性传递一些俗气。 有没有一种更干净的方法来传递整个对象,并从帮助器内部引用属性? 类似于以下内容:
<template name="files">
.
.
<thead>...</thead>
<tbody>
{{#each files}}
<tr class="status" style="background-color:{{getStatusColor this}}">
<td>{{_id}}</td>
<td>{{created_on}}</td>
<td>{{changed_on}}</td>
<td>{{deleted_on}}</td>
</tr>
{{/each}}
</tbody>
然后像这样在助手中引用属性:
Template.files.helpers({
/* returns background color for row based on file properties */
getStatusColor: function(obj) {
if(obj.deleted_on)
return foo...
etc.
}
});
我知道这会使助手需要更多地了解所讨论的数据对象,但是很好奇是否可行。
PS:我对Meteor还是很陌生,请保持温柔。
您只是回答了自己的问题。 如果在each
循环中传递this
,它将传递整个对象。
在模板中:
{{#each files}}
<tr class="status" style="background-color:{{getStatusColor this}}">
<td>{{_id}}</td>
<td>{{created_on}}</td>
<td>{{changed_on}}</td>
<td>{{deleted_on}}</td>
</tr>
{{/each}}
在client.js文件上:
Template.files.helpers({
/* returns background color for row based on file properties */
getStatusColor: function(file) {
if(file.deleted_on)
return foo...
etc.
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.