【发布时间】:2013-07-31 21:44:33
【问题描述】:
我有一个结构如下的车把模板:
{{#each array}}
some stuff
{{#each array2}}
some more stuff
{{#customHelper}}{{/customHelper}}
{{/each}}
{{/each}}
每个数组包含四个项目。
车把助手看起来像这样:
Handlebars.registerHelper('customHelper', function(options){
console.log(options.data);
});
options.data 包含通过 #each 的次数 (index)。如果有多个#each,则返回最里面的一个。但是,我想要最外层的索引#each。
记录options.data 给出:
Object {index: 0, index: 0}
Object {index: 1, index: 0}
Object {index: 2, index: 0}
Object {index: 3, index: 0}
Object {index: 0, index: 1}
Object {index: 1, index: 1}
Object {index: 2, index: 1}
Object {index: 3, index: 1}
Object {index: 0, index: 2}
Object {index: 1, index: 2}
Object {index: 2, index: 2}
Object {index: 3, index: 2}
Object {index: 0, index: 3}
Object {index: 1, index: 3}
Object {index: 2, index: 3}
Object {index: 3, index: 3}
这表明两个索引都存在。第一个索引是最里面的#each,第二个索引是最外面的。据我所知,在一个 javascript 对象中甚至不可能有两个相同的键。
记录options.data.index 给出第一个index,而不是第二个。
是否可以访问第二个index,如果可以,如何访问?
【问题讨论】: