1

I have an array: var eventTypes = [{name: x, value: y}, ... {...}...]; and I'm trying to iterate over this with handlebars. I've tried

{{#each eventTypes
{{/each}}

but no luck. so, how can I iterate over a javascript array?

FIX: Made an Array controller that held the array within it.

Blocks.EventTypesController = Em.ArrayController.extend({
content: eventTypes // controller eventTypes = global eventTypes
});
2
  • 1
    What's inside the {{#each}} block? Is anything printing? Errors? Commented Jan 10, 2014 at 17:45
  • Nothing. I'm looking more into the problem, and I believe it is because eventTypes is not within the context, so handlebars doesn't find the array. I tried Iterating over an array that didn't exist and nothing was shown and no error was thrown as well. Commented Jan 10, 2014 at 17:46

2 Answers 2

1

You need to attach that array to the controller which is backing the template, handlebars works within the scope of the controller, not the global scope.

App.SomeController = Em.Controller.extend({
  eventTypes: eventTypes // controller eventTypes = global eventTypes
});
Sign up to request clarification or add additional context in comments.

1 Comment

He didnt mention ember
0

You will need to use this to get to each object in the array as well.

{{#each eventTypes}}
    {{ this }}      
{{/each}}

2 Comments

Problem is... handlebars is not finding the eventTypes array. I have the array in events.js. How can handlebars find where this array was declared?
then we need more code - how are you trying to pull in the array - can you set up a fiddle?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.