How can you get an item's position in a list?
I'm trying to do something like the following:
Template:
{{#each people}}
{{position}}.- {{name}}
{{/each}
JS:
Template.leaderboard.people = -> Players.find({}, { sort: { rank: 1 } })
Template.leaderboard.position = -> ???
So that if the data on Players is:
[
{ name: "Tom", rank: 1.2 },
{ name: "Dick", rank: 0.7 },
{ name: "Harry", rank: 1.5 }
]
The results will be:
1.- Dick
2.- Tom
3.- Harry
Maybe there's a way to do it with a mongo projection but I can't find how.
Update/Answer:
Template.leaderboard.people = ->
Players.find({}, { sort: { rank: 1 } }).map (doc,index) ->
doc.position = index + 1
doc