我正在尝试根据文档中字段的值来呈现模板。
我尝试在辅助程序中使用开关盒,但返回值不正确。
<template name="unitsList">
{{#each units}}
{{> unitItem}}
{{/each}}
</template>
Template.unitsList.helpers({
units: function() {
return Units.find({}, {sort: {name: 1}});
}
});
<template name="unitItem">
<a href="{{unitType}}">{{name}}</a>
</template>
Template.unitItem.helpers({
unitType: function() {
var unitType = this.unitType;
switch(unitType){
case 'first': return "{{pathFor 'unitPageFirst'}}";
case 'second': return "{{pathFor 'unitPageSecond'}}";
}
}
});
我正在以错误的方式进行操作或缺少基本内容...
我剪出了很多代码来解决这个问题。
关于如何使它正常工作的任何想法,或者关于如何使其更好地工作的任何建议?
您不能在执行时从JS返回未编译的空格键字符串。
您可以使用Router.path
来在模板帮助器中获取路线的路径:
Template.unitItem.helpers({
unitType: function() {
var unitType = this.unitType;
switch(unitType){
case 'first':
return Router.path('unitPageFirst', this);
case 'second':
return Router.path('unitPageSecond', this);
}
}
});
或者,您可以通过声明模板助手来检查来使用纯空格键unitType
。
的HTML
<template name="unitItem">
{{#if unitTypeIs 'unitTypeFirst'}}
<a href="{{pathor 'unitTypeFirst'}}">{{name}}</a>
{{/if}}
{{#if unitTypeIs 'unitTypeSecond'}}
<a href="{{pathor 'unitTypeSecond'}}">{{name}}</a>
{{/if}}
</template>
JS
Template.unitItem.helpers({
unitTypeIs: function(unitType){
return this.unitType == unitType;
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句