Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating a loop counter in Ember.js #each

I'm finding the restrictions of handlebar templates in Ember.js to be absolutely crippling. You can't execute arbitrary javascript. All you get is a fixed set of abilities (if/else, each, action, view, outlet) and the ability to output the value of a property on the current context object.

Which means I cannot figure out how to do something as simple as utilizing a loop counter in order to, for example, slap positional IDs on an html element inside a loop based on it's position in the collection I'm iterating on.

It doesn't make sense to add it as a computed property on the model, because that's not model-level knowledge (the same model could be in two different positions in different views).

FYI-Using Ember-1.0-pre, Ember-Data, and Rails with ActiveModel::Serializers on PostgreSQL.

like image 907
odigity Avatar asked Jul 28 '26 13:07

odigity


2 Answers

With handlebars.js (v. 1.0.rc1) I could do this:

 {{#each links}}
   <li>{{@index}} - {{url}}</li>
 {{/each}}
like image 93
PålOliver Avatar answered Aug 01 '26 01:08

PålOliver


I'd recommend writing a handlebars helper, in which you can execute arbitrary javascript. See http://handlebarsjs.com for some examples.

Alternatively, you could include the positional IDs on your child views (not your models).

like image 21
Dan Gebhardt Avatar answered Aug 01 '26 01:08

Dan Gebhardt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!