Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Index in #each in emberjs

Please check out the code attached:

http://jsbin.com/atuBaXE/2/

I am trying to access the index using {{@index}} but it seems not to be compiling. I think handlebars supports that:

{{#each item in model}}
  {{@index}} 
  {{item}}
{{/each}}

It is not working for me. I can't figure out if the {{@index}} is supported or not.

I am using:

  • Ember.VERSION : 1.0.0
  • Handlebars.VERSION : 1.0.0
like image 273
Mohammad Abu Musa Avatar asked Nov 05 '13 17:11

Mohammad Abu Musa


People also ask

What is an index in Access?

What is an index? You can use an index to help Access find and sort records faster. An index stores the location of records based on the field or fields that you choose to index. After Access obtains the location from the index, it can then retrieve the data by moving directly to the correct location.

What is index () in Python?

Python index() is an inbuilt function in Python, which searches for a given element from the start of the list and returns the index of the first occurrence.


4 Answers

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

like image 95
Marcio Junior Avatar answered Oct 06 '22 14:10

Marcio Junior


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

like image 45
Kingpin2k Avatar answered Oct 06 '22 14:10

Kingpin2k


Note, regarding the @index syntax specifically, as of October 2014:

Ember does not support @index (or any of the other @data type properties).

https://github.com/toranb/ember-template-compiler/issues/16#issuecomment-38823756

like image 3
Feckmore Avatar answered Oct 06 '22 13:10

Feckmore


I like the answer from @kingpin2k--The Ember Way is to use a controller to decorate a model, and in this context we want to decorate it by adding an index property to represent its place in the collection.

I do it slightly differently, by building a separate collection of instance controllers decorated for the task at hand:

App.PostsIndexController = Ember.ArrayController.extend({
  indexedContent: function() {
    get('content').map(function(item, index) {
      App.PostsItemController.create({
        content: item,
        index: index
      });
    });
  }.property('content')
});

App.PostsItemController = Ember.ObjectController.extend({
  index: null
});
like image 2
aceofspades Avatar answered Oct 06 '22 13:10

aceofspades