ManuelDeLeon / viewmodel

MVVM for Meteor
https://viewmodel.org
MIT License
205 stars 23 forks source link

How access @index property on #each #186

Closed dnish closed 8 years ago

dnish commented 8 years ago

Hey, I'm just trying to access the current index of my array to generate a top 100 list with ViewModel. This is the typical Blaze syntax:

{{#each getArray}}
  <div class="item" data-value="{{@index}}">{{this}}</div>
{{/each}}

With ViewModel, I would generate something like this

{{#each getArray}}
  {{>item}}
{{/each}}

But how can I access the @index value in my item VM? My first solution was to add this value to it's context:

{{#each getArray}}
     {{>item position="{{@index}}"}}
{{/each}}

But this would override the whole context of each item.

ManuelDeLeon commented 8 years ago

Also pass the values you need:

{{#each getArray}}
     {{>item position="{{@index}}" prop1=this.prop1}}
{{/each}}

Or the entire object in one shot:

{{#each getArray}}
     {{>item position="{{@index}}" item=this}}
{{/each}}
dnish commented 8 years ago

Ah okay, thank you for the information. Think I will use the seconds solution.