When to use #each User, #each user in controller, #each user in model, #each user in users, etc


#1

I’m confused on how I’m supposed to iterate through arrays of objects and when to use the different possibilities.

So far I’ve come across:

{{#each user in controller}}
{{#each user in model}}
{{#each user in users}}
{{#each User}}

And I’ve even switched some of them up a bit just to see if I could break it; for example both

{{#each user in controller}}

and

{{#each user in model}}

output the same code successfully. I was hoping someone knew a simple exaplanation. Thanks!


#2

Try to read the guides about controllers on the website, especially this one about Ember.ArrayController.

The important point is that controllers can be proxies. This means that when you say {{#each user in controller}}, the controller proxies to it’s model property. That’s why you get the exact same result as {{#each user in model}}

{{#each User}} generally does not make sense. {{#each user in users}} only makes sense when the controller has a users property, which won’t happen automatically just because you use e.g. an App.User model.

Makes sense? :slight_smile:


#3

:+1: with @seilund

I would add that if you have some sortProperties defined on the controller, then {{each controller}} would show a sorted result, whereas {{each model}} won’t be sorted.


#4

See also https://github.com/emberjs/ember.js/commit/9c57efb9dcc1754d805bb401c20652f1e51db1b1


#5

Thanks very much. That does clear it up a bit.