Template.instance() number for an #each loop


#1

Hello,

Is it possible to get the instance number for an #each loop ?

My template is like this :

{{#each homeTeam.players}}
  {{>playerPill}}
{{/each}}

playerPill.html

<template name="playerPill">
  <button class="pillForModal btn btn-primary" data-toggle="modal" data-target="#playerModal" type="button">Numéro <span class="badge">{{jersey}}</span></button>
</template>

The homeTeam.players Array looks like this :

"homeTeam" : {
    "players" : [ 
            {
                "firstName" : null,
                "lastName" : null,
                "jersey" : 1
            }, 
            {
                "firstName" : null,
                "lastName" : null,
                "jersey" : 2
            }, 
            {
                "firstName" : null,
                "lastName" : null,
                "jersey" : 3
            }
    ]
}

What I want if to pass in a modal and next in my update query for the DB the index of the #each loop ?

I know Meteor is currently reviewing the solution for my problem https://github.com/meteor/meteor/pull/3560 but I can’t wait for this solution…

Is it possible via Template.instance() to get the index number for the #each loop ?

Thanks for the help.

Dexterneo


#2

In short, no. It’s a lot more complex an issue than it first looks. However, as you’ve pointed out, there is a pull-request with support for index in Blaze.

There are ways of working around this though; why not assign each player an ID? Then you can use the ID to reference the player in the modal.


#3

Hello,

Yeah I was thinking the same, but was wondering if an elegant solution existed.

Thanks for the help @Siyfion