Skip to content

Instantly share code, notes, and snippets.

@hinok
Created January 18, 2015 14:36
Show Gist options
  • Select an option

  • Save hinok/a3d413004df3e1ff4bb0 to your computer and use it in GitHub Desktop.

Select an option

Save hinok/a3d413004df3e1ff4bb0 to your computer and use it in GitHub Desktop.

Revisions

  1. hinok created this gist Jan 18, 2015.
    77 changes: 77 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    /**
    A replacement for #each that provides an index value (and other helpful values) for each iteration.
    Unless using `foo in bar` format, the item at each iteration will be accessible via the `item` variable.

    Simple Example
    --------------
    ```
    {{#eachIndexed bar in foo}}
    {{index}} - {{bar}}
    {{/#eachIndexed}}
    ```

    Helpful iteration values
    ------------------------
    * index: The current iteration index (zero indexed)
    * index_1: The current iteration index (one indexed)
    * first: True if this is the first item in the list
    * last: True if this is the last item in the list
    * even: True if it's an even iteration (0, 2, 4, 6)
    * odd: True if it's an odd iteration (1, 3, 5)
    */
    Ember.Handlebars.registerHelper('eachIndexed', function eachHelper(path, options) {
    var $ = Ember.$;
    var keywordName = 'item';
    var fn;

    if (arguments.length === 4) {
    // Process arguments
    // #earchIndexed foo in bar
    Ember.assert('If you pass more than one argument to the eachIndexed helper, it must be in the form #eachIndexed foo in bar', arguments[1] === 'in');
    Ember.assert(arguments[0] +' is a reserved word in #eachIndexed', $.inArray(arguments[0], ['index', 'index+1', 'even', 'odd']));

    keywordName = arguments[0];
    options = arguments[3];
    path = arguments[2];
    options.hash.keyword = keywordName;

    if (path === '') {
    path = 'this';
    }
    } else if (arguments.length === 1) {
    // Process arguments
    // #earchIndexed bar
    options = path;
    path = 'this';
    }

    // Wrap the callback function in our own that sets the index value
    fn = options.fn;
    function eachFn() {
    var keywords = arguments[1].data.view._keywords;
    var view = arguments[1].data.view;
    var index = view.contentIndex;
    var list = view._parentView.get('content') || [];
    var len = list.length;

    // Set indexes
    keywords.index = index;
    keywords.index_1 = index + 1;
    keywords.first = (index === 0);
    keywords.last = (index + 1 === len);
    keywords.even = (index % 2 === 0);
    keywords.odd = !keywords.even;
    arguments[1].data.keywords = keywords;

    return fn.apply(this, arguments);
    }
    options.fn = eachFn;

    // Render
    options.hash.dataSourceBinding = path;
    if (options.data.insideGroup && !options.hash.groupedRows && !options.hash.itemViewClass) {
    new Ember.Handlebars.GroupedEach(this, path, options).render();
    } else {
    return Ember.Handlebars.helpers.collection.call(this, Ember.Handlebars.EachView, options);
    }
    });