Javascript 使用变量作为名称访问另一个数组的嵌套元素
我为在助手中构建的数组执行每个循环:Javascript 使用变量作为名称访问另一个数组的嵌套元素,javascript,meteor,spacebars,Javascript,Meteor,Spacebars,我为在助手中构建的数组执行每个循环: Template.article.helpers({ section: function() { return [ {type: 'cars', sectionTitle: 'Cars'}, {type: 'vegetables', sectionTitle: 'Vegetables'} ]; }, }); 文章的数据来自路由器: Router.route('
Template.article.helpers({
section: function() {
return [
{type: 'cars', sectionTitle: 'Cars'},
{type: 'vegetables', sectionTitle: 'Vegetables'}
];
},
});
文章的数据来自路由器:
Router.route('/article/:_id', {
name: 'article',
data: function() {
return {
article: Articles.findOne({ _id: this.params._id })
}
}
});
但是现在我想用helper的类型
访问文章
的子元素。因此,在本例中,每个循环将执行两次:我首先要使用。/article.cars
,然后使用。/article.vegeture
。我希望你能理解我的问题。我想通过助手类型获取子元素的名称:
<template name="article">
{{#each section}}
<h1>{{this.sectionTitle}}</h1>
<ul>
{{#each ../article.type}} <!-- should get '../article.cars' and '../article.vegetable' -->
<li>{{this.title}}</li>
{{/each}}
</ul>
{{/each}}
</template>
{{#每节}
{{this.sectionTitle}}
{{{each../article.type}
- {{this.title}}
{{/每个}}
{{/每个}}
我想使用
type
的内容作为变量名。如果类型是“cars”,那么我想使用。/articles.cars”。这类似于
articles['cars'],这将是
articles[type]的结果。但在《流星》中,这种写作是不可能的。和
articles.type`有些不同。只需使用另一个助手:
s: function(article) {
return article[this.type];
}
并使用空格键发送参数:
{{#each s ../article}}
这似乎是特定于视图引擎的。这是什么视图引擎?@baao我想我需要像obj[name]这样的东西,但在meteor(见标签)中使用了点符号。但是article.type正在查找字段类型。但是我想使用类型的内容作为名称。