Accessing Index in #each in emberjs
Solution 1:
UPDATE
Since this PR, it's now possible to use the each helper with index, taking advance of the new block params syntax. This is available on canary and hopefully will be enabled by default in ember 1.11
{{#each model as |item index|}}
<li>
Index: {{index}} Content: {{item}}
</li>
{{/each}}
Live sample
FOR OLD VERSIONS
You can use {{_view.contentIndex}}
.
{{#each item in model}}
<li>
Index: {{_view.contentIndex}} Content: {{item}}
</li>
{{/each}}
Live sample
Solution 2:
No it doesn't exist in Ember's version of Handlebars, one way is to use an item controller and add a property to it saying whether it's the first or last etc.
App.IndexController = Ember.ArrayController.extend({
itemController: 'itemer'
});
App.ItemerController = Ember.ObjectController.extend({
needs:['index'],
isFirst: function(){
return this.get('color') === this.get('controllers.index.firstObject.color');
}.property('controllers.index.firstObject')
});
http://emberjs.jsbin.com/aPewofu/1/edit