ruby-on-railsbackbone.jscoffeescripthamlc

Iteration over object in backbone coffee-haml template?


I am trying to iterate over a backbone collection object and print one of it's attributes into a unlinked list on a haml-coffee template (.hamlc).

In context, I have a collection of fonts. I want to print the name of each font onto the template.

This is how imagine the code is going to look:

%ul
  - for font in @fonts
    %li
      = font.name

However, this does not iterate at all. I can put any input in this for loop and it simply doesn't execute.

This is what @fonts looks like when output to the console.log

Object {0: Object, 1: Object, 2: Object, 3: Object, 4: Object…}
0: Object
_id: "50bed321dbc554c7a0000005"
data: Object
name: "Actor"
user_ids: Array[0]
__proto__: Object
1: Object
2: Object
3: Object

How do I iterate over this object and output and print each name in the template?


Solution

  • It seems you should specify which attribute of @fonts collection you are about to display like this :

    %ul
     - for font in @fonts.name
       %li
         = font
    

    If you'd like to iterate over object or collection you use :

    - for name, location of @fonts
      = name + "lives in" + location
    

    (for example, in case your fonts object has name and location properties)

    In your code just replace 'in' with 'of' .