2016-02-11 66 views
1

我試圖從{{#each}}循環中將項目的索引傳遞給動態模板,但是在如何獲得它(以乾淨的方式)時丟失了。流星#每個@index傳遞給動態模板?

當前代碼:

{{#each item}} 
{{Template.dynamic template=type data=this}} 
{{/each}} 

有了這個,{{@index}}是不是在動態加載的模板訪問。

我也試過使用模板助手,但沒有出現在上下文中跟蹤索引。

{{#each item}} 
{{Template.dynamic template=type data=itemData}} 
{{/each}} 

Template.items.helpers({ 
    itemData() { 
    // can't access index in here 
    return this; 
    } 
}); 

任何人都可以建議我怎麼能做到這一點?

謝謝!

回答

1

解決了這個模式如下:

... Template.Items 
{{#each items}} 
{{>Template.dynamic itemConfig @index}} 
{{/each}} 

Template.items.helpers({ 
    itemConfig(index) { 
    const data = this; 
    data.index = index; 

    return { 
     data, 
     template: this.type //this.type is where im storing my template name 
    }; 
    }, 
}); 

使用@index作爲輔助PARAM,然後大火使用對象作爲動態模板配置!

:)

編輯:我發現another solution。做同樣的工作,我更喜歡它的外觀。

{{>Template.dynamic template=type data=(templateData @index)}} 

哪裏templateData基本上是從之前相同的助手,但只是返回dataindex道具。

0
{{#each item}} 
{{Template.dynamic template=type [email protected]}} 
{{/each}} 

您可以使用「索引」的動態模板訪問索引