javascript - In Meteor how to pass entire object to template helper? -
i have template helper using set color of table row, based on logic. helper requires several fields make determination.
<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>
and (abbreviated) js:
template.files.helpers({ /* returns background color row based on file properties */ getstatuscolor: function(deleted_on, changed_on, created_on) { if(deleted_on) return foo... if(changed_on) return bar... } });
this works great seems kind of cheesy need pass each individual attribute. there cleaner way pass entire object, , reference attributes within helper? along lines of:
<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>
and referencing attributes instead in helper this:
template.files.helpers({ /* returns background color row based on file properties */ getstatuscolor: function(obj) { if(obj.deleted_on) return foo... etc. } });
i understand make helper need know more data object in question, curious if possible.
p.s. i'm quite new meteor, gentle.
you answered own question. if pass in this
inside each
loop, pass in whole object.
in template:
{{#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}}
on client.js file:
template.files.helpers({ /* returns background color row based on file properties */ getstatuscolor: function(file) { if(file.deleted_on) return foo... etc. } });
Comments
Post a Comment