我有一个结构如下的车把模板:
{{#each array}}
some stuff
{{#each array2}}
some more stuff
{{#customHelper}}{{/customHelper}}
{{/each}}
{{/each}}
每个数组包含四个项目。
车把助手看起来像这样:
Handlebars.registerHelper('customHelper', function(options){
console.log(options.data);
});
options.data包含它通过的次数 ( index) #each。如果有多个#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 对象中甚至不可能有两个相同的键。
loggingoptions.data.index给出第一个index,而不是第二个。
是否可以访问第二个index,如果可以,如何访问?