Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way of getting the index of an array when iterating over it using Ember Handlebars?

{#each controller.content.assetAllocation.class}}
    {{@index}}
{{/each}}

I'm trying to run the code above, which is supposed to output the index of the array, but it produces an error saying: "Uncaught SyntaxError: Unexpected token , "

like image 698
JJJ Avatar asked Feb 17 '14 23:02

JJJ


2 Answers

Solution is not as nice as I'd hoped, but this works:

{#each controller.content.assetAllocation.class}}
    {{_view.contentIndex}}
{{/each}}
like image 139
JJJ Avatar answered Nov 09 '22 06:11

JJJ


Here's my way:

{#each controller.content.assetAllocation.class as |item index|}}
    {{index}} - {{item}}
{{/each}}

The index is Zero-based numbering. So if you want to change it, just add an helper like this:

Ember.Handlebars.registerBoundHelper("indexBase1", function (value, options) {
        return value + 1;
});

And using it:

{#each controller.content.assetAllocation.class as |item index|}}
    {{indexBase1 index}} - {{item}}
{{/each}}
like image 44
Trong Mac Avatar answered Nov 09 '22 04:11

Trong Mac