edit this page - page history - about editing

Adding an Index to an each iteration over a computed property in EmberJS

EmberJS

In normal EmberJS you can't do something like this (note that this is possible in Handlebars, but not EmberJS's version of Handlebars):

{{#each foo in bazFoos}}
  {{foo}} index: {{index}}, first: {{first}}
{{/each}}

However, you can create an amazing hack to the computed property to add this information in:

App.MyModel = DS.Model.extend
  bazFoos: (->
    @get("foos").filterBy("bar", "baz").map (item, index, enumerable) ->
      item.set('index', index)
      item.set('first', index == 0)
  ).property("foos")

Categories: Javascript | EmberJS
edit this page - what links to here? - page history - top
Last edited by jevon jevon 40 months ago