2016-01-31 10 views
5

Anche se questo è reso come previsto:Perché annidati ciascuno in uscita modello nulla

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

se metto insieme:

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

Il #each types è vuota.

Gli helper di template:

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'); 
}); 

E una parte del codice modello:

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

Anche in caso di sostituzione {{#each restMenuSideItems}} da {{#each ../restMenuSideItems}}, non appare nulla.

Cosa c'è che non va?

risposta

3

Poiché la clausola #each modifica il contesto dei dati sull'elemento corrente.

Se si desidera un elenco di types all'interno di ciascuno degli Items, è possibile ottenere il contesto dei dati principali utilizzando ...

Se types è un attributo del contesto genitore:

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

Se types è un modello di supporto, è possibile passare il contesto genitore come argomento ad esso:

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

e utilizzare il contesto per la tua richiesta.

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

Grazie, ma è sempre lo stesso. –

+0

Strano, dovrebbe funzionare. Puoi fornire più codice (ad es. I tuoi aiutanti e un codice modello più completo)? Da dove provengono 'Items' e' types'? – MasterAM

+0

Proprio fatto. @MasterAM –