Why does the setupController function need to call controllerFor in order to properly set up the model?
Ember makes URLs a very integral part of its conventions. This means that the state
of your application is represented by the route it is on. You've grokked most of this correctly. But there are couple of subtle nuances, that I will clarify below.
First consider an app with the following URLs,
- /posts - shows a list of blog posts.
- /posts/1 - shows a single blog post.
And say clicking on a post in the list at /posts
takes you to /posts/1
.
Given this scenario, there 2 ways a user will get to see the post at /posts/1
.
- By going to
/posts
and clicking on the 1st post. - By typing in
/posts/1
, via bookmarks etc.
In both these cases, the PostRoute
for /posts/1
will need the model corresponding to Post
id 1.
Consider the direct typing scenario first. To provide a way to lookup the id=1 post model, you would use,
model: function(params) {
return App.Post.find(params.post_id);
}
Your template for post will get the model and it can render using it's properties.
Now consider the second scenario. Clicking on post with id=1 takes you to /posts/1
. To do this your template would use linkTo
like this.
{{#linkTo 'post' post}} {{post.title}} {{/linkTo}}
Here you are passing in the post
model to the linkTo
helper. It then serializes the data for the post into a URL, ie:- '/posts/1'. When you click on this link Ember realizes that it needs to render the PostRoute
but it already has the post model. So it skips the model hook and directly calls setupController
.
The default setupController
is setup to simply assign the model on the controller. It's implemented to do something like,
setupController: function(controller, model) {
controller.set('model', model);
}
If you do not need to set custom properties on your controller, you don't need to override it. Note: if you are augmenting it with additional properties you still need to call _super
to ensure that the default setupController behaviour executes.
setupController: function(controller, model) {
this._super.apply(this, arguments);
controller.set('customProp', 'foo');
}
One final caveat, If you are using linkTo
and the route does not have dynamic segments
, then the model hook is still called. This exception makes sense if you consider that you were linking to the /posts
route. Then the model hook has to fire else Ember has no data to display the route.
Which brings us to the crux of your question. Nearly there, I promise!
In your example you are using linkTo
to get to the EventsNewRoute
. Further your EventsNewRoute
does not have dynamic segments so Ember does call the model
hook. And controller.set("model", model);
does work in so much as setting the model on the controller
.
The issue is to do with your use of renderTemplate
. When you use render
or {{render}}
helper inside a template, you are effectively getting a different controller to the one you are using. This controller is different from the one you set the model
on, hence the bug.
A workaround is to pass the controller in the options, which is why renderTemplate
gets this controller as an argument.
renderTemplate: function(controller) {
this.render("events-new", {
outlet : "events-new", controller: controller
});
}
Here's an updated jsfiddle.
Final Note: Unrelated to this question, you are getting the warning,
WARNING: The immediate parent route ('application') did not render into the main outlet and the default 'into' option ('events') may not be expected
For that you need to read this answer. Warning, it's another wall of text! :)