Ember.js 1.0 RC: workaround for the itemController issue
Suppose you have a PostsController
and a PostController
. When you are looping through posts, you want them to be wrapped in a PostController
.
{{#each posts itemController="post"}}
...
{{/each}}
You will run into problems if you want to use the same PostController
for the PostRoute
:
@resource 'posts'
@resource 'post', path: '/posts/:post_id'
If you go to the posts
route, then to an individual post route (/posts/1
) and then go back to the posts
route, all your posts will be using the same instance of the PostController
, which means all your posts will have the same title, id, etc. This issue is discussed on Github.
A quick workaround would be to do this: create initializers/postItemController.js.coffee
:
Ember.Application.initializer
name: 'postItemController'
initialize: (container) ->
App.register('controller', 'postItem', App.PostController)
And then use the PostItemController
in your template:
{{#each posts itemController="postItem"}}
...
{{/each}}
The tip is extracted from @tchak's jsffidle, all credit goes to him!
You're the 10931st person to read this article.