2015-12-11 39 views
2

我是Meteor.js的新手,遇到了問題。流星:Spacebars每個參數

我傳遞一個用戶對象的配置文件模板例如爲:

{ 
    _id: "D8JpXRQskm3grykjg", 
    username: "foo", 
    profile: {communities: ["AkGCakz6mSgMb8qyS", "j8aB3i5iscrC4ehkA"]}, 
} 


<template name="profile"> 
    <h1> {{username}}: {{_id}} </h1> 
    <h3>Communities</h3> 
    <hr> 
    {{#each profile.communities}} 
    {{> communityItem}} 
    {{/each}} 
</template> 

問題是我已經寫了,我在其他地方使用該接受COMMUNITYNAME一個communityItem模板。有沒有一種方法可以編寫一個輔助函數,傳遞將返回社區名稱列表的communityIds列表?我想:

... 
{{#each getCommunityNames(profile.communities)}} 
    {{> communityItem}} 
{{/each}} 
... 

我能很好地接近問題的錯誤的方式或者不在「Spacebars」時尚寫作。謝謝!

回答

1

確保您可以:

Template.myTemplate.helpers({ 
    getCommunityNames: function(commIds) { 
    var communities = Communities.find({_id: {$in: commIds}}).fetch(); 
    return _.pluck(communities, 'name'); // returns ['Name 1', 'Name 2']; 
    } 
}); 

注意,語法方法PARAM沒有方法(PARAM)

{{#each getCommunityNames profile.communities}} 
    {{>communityItem}} 
{{/each}} 
+1

很好,謝謝!它爲我工作:) –