Meteor 和 Iron Router 中的渲染模板取决于文档中的值
Rendering Template in Meteor and Iron Router depending on value in document
我正在尝试根据文档中字段的值呈现模板。
我尝试在助手中使用 switch case,但 return 值不正确。
units_list.html
<template name="unitsList">
{{#each units}}
{{> unitItem}}
{{/each}}
</template>
units_list.js
Template.unitsList.helpers({
units: function() {
return Units.find({}, {sort: {name: 1}});
}
});
unit_item.html
<template name="unitItem">
<a href="{{unitType}}">{{name}}</a>
</template>
unit_item.js
Template.unitItem.helpers({
unitType: function() {
var unitType = this.unitType;
switch(unitType){
case 'first': return "{{pathFor 'unitPageFirst'}}";
case 'second': return "{{pathFor 'unitPageSecond'}}";
}
}
});
我要么以错误的方式解决这个问题,要么遗漏了一些基本的东西...
我删掉了很多代码来专注于这个问题。
关于如何让它工作的任何想法,或者关于如何做得更好的任何建议?
您不能在执行时从 JS return 未编译的空格键字符串。
您可以使用 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;
}
});
查看 Iron-router 指南中的渲染模板,特别是 this.render('xyz'); 语句
https://github.com/iron-meteor/iron-router/blob/devel/Guide.md#rendering-templates
我正在尝试根据文档中字段的值呈现模板。
我尝试在助手中使用 switch case,但 return 值不正确。
units_list.html
<template name="unitsList">
{{#each units}}
{{> unitItem}}
{{/each}}
</template>
units_list.js
Template.unitsList.helpers({
units: function() {
return Units.find({}, {sort: {name: 1}});
}
});
unit_item.html
<template name="unitItem">
<a href="{{unitType}}">{{name}}</a>
</template>
unit_item.js
Template.unitItem.helpers({
unitType: function() {
var unitType = this.unitType;
switch(unitType){
case 'first': return "{{pathFor 'unitPageFirst'}}";
case 'second': return "{{pathFor 'unitPageSecond'}}";
}
}
});
我要么以错误的方式解决这个问题,要么遗漏了一些基本的东西...
我删掉了很多代码来专注于这个问题。
关于如何让它工作的任何想法,或者关于如何做得更好的任何建议?
您不能在执行时从 JS return 未编译的空格键字符串。
您可以使用 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;
}
});
查看 Iron-router 指南中的渲染模板,特别是 this.render('xyz'); 语句
https://github.com/iron-meteor/iron-router/blob/devel/Guide.md#rendering-templates