Why nested in the template do not display anything

While this is being displayed as expected:

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

if i break it:

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

#each types empty.

Template Helpers:

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

And some of the template code:

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

Even when replacing {{#each restMenuSideItems}} with {{#each ../restMenuSideItems}} nothing appears.

What's wrong?

+5
source share
1 answer

Because the #each changes the data context to the current element.

If you need a types list inside each of the Items , you can get the context of the parent data using ..

If types is an attribute of the parent context:

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

If types is a template helper, you can pass the parent context as an argument to it:

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

and use the context for your request.

 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. } }); 
+3
source

Source: https://habr.com/ru/post/1241926/


All Articles