2013-07-31 32 views
0

多個索引元件我有構成這樣的車把模板:options.data在車把輔助

{{#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對象中不應該有兩個相同的鍵。

日誌記錄options.data.index給出第一個index,而不是第二個。

是否可以訪問第二個index,如果是這樣,怎麼辦?

回答

0

我想通了。 刪除索引:

delete options.data.index; 

將只刪除了第一個,所以現在

options.data.index 

將返回的外#each

指數