【发布时间】:2014-05-14 05:05:09
【问题描述】:
我正在创建一个 Meteorite 包,它为特定集合呈现一个表格。我希望通过添加可选的自定义列作为模板来自定义此表:
<template name="myPluginTable">
<table>
<thead>
<th>Column 1</th>
<th>Column 2</th>
{{> customHeaders}}
</thead>
<tbody>
{{#each objects}}
<tr>
<td>{{firstValue}}</td>
<td>{{secondValue}}</td>
{{> customFields}}
</tr>
{{/each}}
</tbody>
</table>
</template>
现在只要在应用程序的某处指定模板customHeaders 和customFields,它就可以正常工作。
但是如果省略这些模板,则会引发错误。这也不能通过向包中添加虚拟模板来规避,因为尝试覆盖它们也会引发错误。
我想出的解决方案:
Template.myPluginTable.helpers({
customHeadersExist: function() {
return typeof(Template.customHeaders) === "object";
},
customFieldsExist: function(user) {
return typeof(Template.customFields) === "object";
},
});
在模板中:
{{#if customHeadersExist}}
{{> customHeaders}}
{{/if}}
和
{{#if customFieldsExist}}
{{> customFields}}
{{/if}}
现在这个工作,但是对于这样一个简单的任务,解决方案似乎过于复杂——我真的需要为每个可选模板声明辅助函数吗?所以我的问题真的是:
使包自定义模板可选的最佳方法是什么?是这样,还是有更好的方法?
【问题讨论】:
标签: javascript meteor spacebars