Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access outer {{#each}} collection value in the nested loop

Tags:

meteor

What is the standard way to access outer #each collection values in the loop? for example:

<template name="example">
  {{#each outerCollection}}
  <tr>
    {{#each innerCollection}}
      <td>{{aaa}}</td>
    {{/each}}
  </tr>
  {{/each}}
</template>

Template.example.aaa = function(){
  // cannot access outerCollection values
}

in above Template.example.aaa, this points to the inner collection.

I cannot find way to access outerCollection items. My solution is like below, I am defining my own helper function. Is it a standard Meteor way to achieve this purpose?

<template name="example">
  {{#each outerCollection}}
  <tr>
    {{#each innerCollection}}
      <td>{{myHelper ../outerItem innerItem}}</td>
    {{/each}}
  </tr>
  {{/each}}
</template>

Handlebars.registerHelper('myHelper', function (outItem, inItem) {
  // can access outerCollection via outerItem
});

I found a similar question for the case of inner event handler access.

like image 762
hyde Avatar asked Dec 03 '12 16:12

hyde


People also ask

How do you access the outer class variable in an inner class?

If you want your inner class to access outer class instance variables then in the constructor for the inner class, include an argument that is a reference to the outer class instance. The outer class invokes the inner class constructor passing this as that argument.

Can inner class access outer class methods?

Any non-static nested class is known as inner class in java. Java inner class is associated with the object of the class and they can access all the variables and methods of the outer class.

Can outer Java class access inner class private members?

If the inner class defined as private and protected, can outer class access the members of inner class? Yes.


2 Answers

You can use below code to fetch outer collections.

suppose you have collection called as Collection.Customer and Collection.RechargePlan and you are using both in a template for updating Customer.

Customer = {"name":"James", "rechargeplan":"monthly"};
RechargePlan = [{"rechargeplan": "monthly"},{"rechargeplan": "yearly"}];

 //Inside template, Bydefault Customer is available.
{{#each RechargePlan}}
  {{#if equals ../rechargeplan rechargeplan}}
      //Hurray Plan matches
  {{/if}}
{{/each}}

In above code, ../rechargeplan is actually Customer.rechargeplan, ../ actually went one step above heirarchy and then accessed the field if available, since Customer is already available to template, it's field is picked up.

like image 25
Ankur Soni Avatar answered Oct 13 '22 19:10

Ankur Soni


I think you've answered this yourself! Using ../ is documented in https://github.com/meteor/meteor/wiki/Handlebars.

like image 180
David Glasser Avatar answered Oct 13 '22 19:10

David Glasser