2014-12-29 37 views
0

在這種Example 2 - 「傳遞數據上下文流星數據上下文,傳遞陣列到每個Spacebars環

JS

Template.overview.helpers({ 
    users: [ 
    { name: 'David' }, 
    { name: 'Shaune' } 
    ] 
}); 

HTML

<template name="overview"> 
    {{> userList users}} 
</template> 

<template name="userList"> 
    {{#each this}} 
    {{name}}<br> 
    {{/each}} 
</template> 

結果

David 
Shaune 

我的目標是模板名稱數組傳遞到Template.overview,用戶是,像這樣:

<template name="overview"> 
    {{#each templateArray }} 
    {{> userList this }} 
    {{/each}} 
</template> 

哪裏templateArray與模板名稱的字符串數組,this是名字的每個模板。

這可能嗎?

編輯:

對於更深入,認爲我已經在我的helpers

Template.overview.helpers({ 
    users: [ 
    { name: 'David' }, 
    { name: 'Shaune' } 
    ], 
    otherUsers: [ 
    { name: 'Jane' }, 
    { name: 'Betty' } 
    ], 
    moarUsers: [ 
    { name: 'Ben' }, 
    { name: 'Jerry' } 
    ] 
}); 

下面我想這些數據上下文的傳遞到相同的模板(在這種情況下, userLists)。

回答

1

你並不需要調用任何東西,你可以使用this內幫手,就像

helper: function(){ 
    return this; //if you pass string via templateArray 
    return this.name; //if you pass object via templateArray 
} 

因爲你做了什麼看起來是這樣,但只是很好地包裹成模板

<template name="overview"> 
    {{#each templateArray }} 
     {{name}}<br> 
    {{/each}} 
</template> 

我不知道你爲什麼在代碼中有雙循環,在第一個循環中你傳遞對象並且它不能遍歷它

編輯

如果你想字符串只是循環throught數組,這樣做:

{{#each templateArray}} 
     {{this}}<br> 
{{/each}} 
+0

目標是使許多模板(因爲它們在每種情況下,唯一的數據上下文),從而提供一個參數{{>用戶列表}}是我對此的假設。用戶助手是一個對象數組,我有許多這樣的數組。 – DeBraid

+0

但是你想給他們提供他們已經擁有的'this',在你上面的例子中,你可以在任何地方在userList模板代碼或'{{name}}''中的任何地方使用'this.name' – Sindis