2016-01-31 16 views
5

に各ネストん:はなぜテンプレートの出力は何も

{{#each Items}} // a collection 
    {{title}} 
{{/each}} 
{{#each types}} // another ollection 
    {{refId}} 
{{/each}} 

私はそれを一緒に置く場合:

{{#each Items}} 
    {{title}} 
    {{#each types}} 
    {{refId}} 
    {{/each}} 
{{/each}} 

#each typesは空です。

テンプレートヘルパー:

Template.menuItems.helpers({ 
    restMenuItems: function() { 
     return RestMenuItems.find({restRefId: this._id}, {sort:Template.instance().sort.get()}); 
    }, 
    restMenuSideItems: function() { 
     return RestMenuSideItems.find({restRefId: this._id},   {sort:Template.instance().sort.get()}); 
    } 
}); 

Template.menuItems.onCreated(function(){ 
    this.subscribe('restMenuItems'); 
    this.subscribe('restMenuSideItems'); 
}); 

とテンプレートコードの一部:

{{#each restMenuItems}} 
    <div> 
    <select class="list-group select_side_addons"> 
     {{#each restMenuSideItems}} 
     <option>{{restRefId}}</option> 
     {{/each}} 
    </select> 
    </div> 
{{/each}} 

{{#each ../restMenuSideItems}}{{#each restMenuSideItems}}を交換しても、何も表示されません。

どういうところが間違っていますか?

答えて

3

#each句は、データコンテキストを現在の項目に変更するためです。

Itemsのそれぞれにtypesのリストが必要な場合は、..を使用して親データコンテキストを取得できます。

typesの場合は、親コンテキストの属性である:

{{#each Items}} 
    {{title}} 
    {{#each ../types}} 
    {{refId}} 
    {{/each}} 
{{/each}} 

typesは、テンプレートヘルパーの場合、あなたはそれを引数として親コンテキストを渡すことができます。

{{#each Items}} 
    {{title}} 
    {{#each types ..}} 
    {{refId}} 
    {{/each}} 
{{/each}} 

と使用しますクエリのコンテキスト。

Template.myTemplate.helpers({ 

    types: function(parent) { 
    // you now have the parent context here. 
    // use parent._id etc. where you used this._id in the 
    // "flat" version. 
    } 
}); 
+0

ありがとうございますが、それでも同じです。 –

+0

奇妙な、それはうまくいくはずです。より多くのコード(例:ヘルパーや完全なテンプレートコード)を提供できますか? 'Items'と' types'はどこから来たのですか? – MasterAM

+0

ちょうどしました。 @MasterAM –

関連する問題