为什么嵌套模板输出中的每一个都没有

Avi*_*i A 5 javascript meteor spacebars meteor-blaze

虽然这是按预期呈现的:

{{#each Items}}  // a collection
  {{title}}  
{{/each}}  
{{#each types}}  // another ollection
  {{refId}}  
{{/each}}  
Run Code Online (Sandbox Code Playgroud)

如果我把它放在一起:

{{#each Items}}  
  {{title}}  
  {{#each types}}  
    {{refId}}  
  {{/each}} 
{{/each}} 
Run Code Online (Sandbox Code Playgroud)

#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');
});
Run Code Online (Sandbox Code Playgroud)

还有一些模板代码:

{{#each restMenuItems}}
  <div>
    <select class="list-group select_side_addons">
      {{#each restMenuSideItems}}
        <option>{{restRefId}}</option>
      {{/each}}
    </select>
  </div>
{{/each}}
Run Code Online (Sandbox Code Playgroud)

即使更换时{{#each restMenuSideItems}}通过{{#each ../restMenuSideItems}},似乎什么都没有.

怎么了?

Mas*_*rAM 3

因为该#each子句将数据上下文更改为当前项。

types如果您想要每个内部的列表Items,您可以使用 获取父数据上下文..

如果types是父上下文的属性:

{{#each Items}}  
  {{title}}  
  {{#each ../types}}  
    {{refId}}  
  {{/each}} 
{{/each}} 
Run Code Online (Sandbox Code Playgroud)

如果types是模板助手,您可以将父上下文作为参数传递给它:

{{#each Items}}  
  {{title}}  
  {{#each types ..}}  
    {{refId}}  
  {{/each}} 
{{/each}} 
Run Code Online (Sandbox Code Playgroud)

并使用您的查询的上下文。

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.
  }
});
Run Code Online (Sandbox Code Playgroud)