Javascript 在Meteor中,如何将整个对象传递给模板帮助器?
目前,我有一个模板助手,用于根据某些逻辑设置表行的颜色。辅助对象需要几个字段来进行确定Javascript 在Meteor中,如何将整个对象传递给模板帮助器?,javascript,meteor,Javascript,Meteor,目前,我有一个模板助手,用于根据某些逻辑设置表行的颜色。辅助对象需要几个字段来进行确定 <template name="files"> . . <thead>...</thead> <tbody> {{#each files}} <tr class="status" style="background-color:{{getStatusColor deleted_on chang
<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>
这非常有效,但我需要传递每个属性,这似乎有点俗气。是否有更干净的方法传递整个对象,并从辅助对象中引用属性?大致如下:
<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>
我理解这会使助手需要更多地了解所讨论的数据对象,但我很好奇这是否可能
顺便说一句,我对Meteor还不太熟悉,温柔点。你刚刚回答了你自己的问题。如果在
每个循环中传入此,它将传入整个对象
在模板中:
{{#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}}
哈,你说得对(我想我也是)。我的问题是,大约在同一时间,我在服务器端的pub上做了一个无关的更改,使得我要查找的字段不可用。愚蠢的错误。很高兴知道这是可能的。我在Meteor文档中没有看到关于模板助手的“this”引用,只是猜测它应该可以工作。
Template.files.helpers({
/* returns background color for row based on file properties */
getStatusColor: function(obj) {
if(obj.deleted_on)
return foo...
etc.
}
});
{{#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}}
Template.files.helpers({
/* returns background color for row based on file properties */
getStatusColor: function(file) {
if(file.deleted_on)
return foo...
etc.
}
});