mavilein
mavilein

Reputation: 11668

Why is my {{#each}} not working?

Why is the Ember Snippet i have linked not working? I am setting up a simple ArrayController and fill it with contents upon initialization. Then i want to display the contents of this controller with the help of {{#each}}, but this is not working.

In tutorials i have read through, the following structure is always used:

{{#each  AppNamespace.myModelController}}
...
{{each}}

But to make my example work i had to use:

{{#each  AppNamespace.myModelController.content}}
...
{{/each}}

Could you have a look at the provided fiddle and tell me what is wrong with it? I assume that i must have done something wrong since i have seen this pattern so often in tutorials.

Note: I am a Javascript beginner coming from Java Server Development. So it maybe easy JS basics that i am struggling with.

I would have posted the complete code here, but the formatting was not working properly.

Link to my JS Fiddle showing my problem

Upvotes: 0

Views: 197

Answers (4)

chrixian
chrixian

Reputation: 2811

If you pass 1 argument to the #each helper it needs to be a Ember.Array compatible object-- in your first example you pass the controller when your data is in the content property.. your second example works because you pass the content property.. and there is nothing wrong with doing it that way if you feel like it.

There is however an alternate syntax for the #each helper which you may see more often, it uses element naming {{#each model in myModelController}} ... {{/each}}. When you do it this way Ember takes care of looking for the content property for you because it's the default rendering context for the view.

Assuming you're using the latest version from Github you'll find it takes care of looking for content in both cases so the point becomes moot once that is released as stable.

Upvotes: 0

Alex Cason
Alex Cason

Reputation: 116

I'm starting to look at Ember myself and I'm concerned about the process you're using.

As far as I'm aware you shouldn't really be retrieving data directly from the controller like that. The pattern you use should be based upon models, controllers, views and the router.

http://trek.github.com/ is a resource which I have found useful while learning about Ember.

Based upon that example, this would be my take on a small ember test application:

http://jsfiddle.net/zDfBv/

Hopefully that will be of some use to you as a starting point.

Upvotes: 1

Jason P
Jason P

Reputation: 1415

Add a call to this._super() inside your init method.

https://github.com/emberjs/ember.js/pull/1251

Also, not directly related to your question, but it looks like you would benefit from reading #6 here: http://codebrief.com/2012/03/eight-ember-dot-js-gotchas-with-workarounds/

Upvotes: 3

Amareswar
Amareswar

Reputation: 2064

Tried your fiddle with http://cloud.github.com/downloads/emberjs/ember.js/ember-0.9.6.min.js instead of 1.0 pre it is working fine in both the cases.

Upvotes: 1

Related Questions