# Should I use setupController or model from my Routes

**URL:** https://discuss.emberjs.com/t/should-i-use-setupcontroller-or-model-from-my-routes/1970
**Category:** Design
**Created:** [July 30, 2013, 2:54pm UTC](https://discuss.emberjs.com/t/should-i-use-setupcontroller-or-model-from-my-routes/1970 "2013-07-30T14:54:57Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![matthewrudy](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/matthewrudy/32/14904_2.png) [@matthewrudy](https://discuss.emberjs.com/u/matthewrudy)
#### Post date: [July 30, 2013, 2:54pm UTC](https://discuss.emberjs.com/t/should-i-use-setupcontroller-or-model-from-my-routes/1970/1 "2013-07-30T14:54:57Z")

</div>

In this [simple example](https://gist.github.com/matthewrudy/6113563) I have a nested route `/user/:user_id/posts`.

I want my `UserPostsController` to be passed `user.get('posts')` as it’s `model`.

The `UserController` already has this user loaded up as it’s own `model`, so I just want to say `@controllerFor('user').get('model.posts')`

However, I have two ways of doing this;

```
model: ->
  @controllerFor('user').get('model.posts')

```

or

```
setupController: (controller, model) ->
  userPosts = @controllerFor('user').get('model.posts')
  controller.set('model', userPosts)

```

Which of these is the better approach?

I would assume that these were in effect the same, but the former approach doesn’t work as expected (when I switch to a new `user` the model doesn’t get updated)

Is the failure of the `model:` approach a bug, or is it expected?

---

<div class="post-metadata">

### Author: ![Spencer\_Price](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/spencer_price/32/14878_2.png) [@Spencer\_Price](https://discuss.emberjs.com/u/Spencer_Price)
#### Post date: [July 30, 2013, 4:28pm UTC](https://discuss.emberjs.com/t/should-i-use-setupcontroller-or-model-from-my-routes/1970/2 "2013-07-30T16:28:58Z")

</div>

There’s another solution that I use that seems to work well for me:

```
App.UserPostsController = Ember.Route.extend({
  model : function() {
    return this.modelFor("user").get("posts");
  }
});

```

---

<div class="post-metadata">

### Author: ![matthewrudy](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/matthewrudy/32/14904_2.png) [@matthewrudy](https://discuss.emberjs.com/u/matthewrudy)
#### Post date: [July 30, 2013, 4:44pm UTC](https://discuss.emberjs.com/t/should-i-use-setupcontroller-or-model-from-my-routes/1970/3 "2013-07-30T16:44:50Z")

</div>

@Spencer_Price I think you made a mistake.

Is that supposed to extend `Ember.Controller`?

Because I tried variants of this

```
App.UserPostsController = Ember.Controller.extend
  model: ->
    @modelFor('user').get('posts')

```

And

```
App.UserPostsController = Ember.ArrayController.extend
  ...

```

But they both error out.

> Assertion failed: an Ember.CollectionView’s content must implement Ember.Array

And

> Uncaught TypeError: Object function () { return this.modelFor(“user”).get(“posts”); } has no method ‘addArrayObserver’

---

<div class="post-metadata">

### Author: ![Spencer\_Price](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/spencer_price/32/14878_2.png) [@Spencer\_Price](https://discuss.emberjs.com/u/Spencer_Price)
#### Post date: [July 30, 2013, 4:56pm UTC](https://discuss.emberjs.com/t/should-i-use-setupcontroller-or-model-from-my-routes/1970/4 "2013-07-30T16:56:16Z")

</div>

Oh…Whoops! So sorry…it should be:

```
App.UserPostsRoute = Ember.Route.extend({
  model : function() {
    return this.modelFor("user").get("posts");
  }
});

```

---

<div class="post-metadata">

### Author: ![matthewrudy](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/matthewrudy/32/14904_2.png) [@matthewrudy](https://discuss.emberjs.com/u/matthewrudy)
#### Post date: [July 30, 2013, 5:36pm UTC](https://discuss.emberjs.com/t/should-i-use-setupcontroller-or-model-from-my-routes/1970/5 "2013-07-30T17:36:41Z")

</div>

Makes sense @Spencer_Price. I get a bug with this though.

If I go to `/user/1/posts` then go to `/user/2/posts` the posts don’t refresh.

When I use `setupController` I don’t have this problem.

---

<div class="post-metadata">

### Author: ![Spencer\_Price](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/spencer_price/32/14878_2.png) [@Spencer\_Price](https://discuss.emberjs.com/u/Spencer_Price)
#### Post date: [July 30, 2013, 5:51pm UTC](https://discuss.emberjs.com/t/should-i-use-setupcontroller-or-model-from-my-routes/1970/6 "2013-07-30T17:51:14Z")

</div>

How are the route transitions being triggered?

---

<div class="post-metadata">

### Author: ![rmbrad](https://avatars.discourse-cdn.com/v4/letter/r/e9c0ed/32.png) [@rmbrad](https://discuss.emberjs.com/u/rmbrad)
#### Post date: [July 30, 2013, 7:14pm UTC](https://discuss.emberjs.com/t/should-i-use-setupcontroller-or-model-from-my-routes/1970/7 "2013-07-30T19:14:32Z")

</div>

The model hook is only called when coming in from a URL, not when using any of the transitionTo methods or the linkTo helper. The setupController hook, on the other hand, is always called no matter how the route was entered.

I guess I should add that the model hook is passed the URL parameters allowing you to resolve them to the actual model object.

---

<div class="post-metadata">

### Author: ![Spencer\_Price](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/spencer_price/32/14878_2.png) [@Spencer\_Price](https://discuss.emberjs.com/u/Spencer_Price)
#### Post date: [July 30, 2013, 8:23pm UTC](https://discuss.emberjs.com/t/should-i-use-setupcontroller-or-model-from-my-routes/1970/8 "2013-07-30T20:23:00Z")

</div>

> [@rmbrad](#):
>
> The model hook is only called when coming in from a URL…

Is this true? Isn’t that only if the route has dynamic segments? If not, and no model is passed in the linkTo helper, it seems the model hook _is_ called.

If this isn’t how it’s _supposed_ to work, I have some code to change.

---

<div class="post-metadata">

### Author: ![kingpin2k](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/kingpin2k/32/4255_2.png) [@kingpin2k](https://discuss.emberjs.com/u/kingpin2k)
#### Post date: [July 30, 2013, 8:54pm UTC](https://discuss.emberjs.com/t/should-i-use-setupcontroller-or-model-from-my-routes/1970/9 "2013-07-30T20:54:48Z")

</div>

Do the model hook. I’m almost positive if you attempt to do modelFor(‘userPosts’) deeper in the route it won’t exist if you don’t return it from the model hook. You could still grab the controller and model from the controller tho. (I gotta go test this and make sure I’m not lying)

---

<div class="post-metadata">

### Author: ![machty](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/machty/32/13933_2.png) [@machty](https://discuss.emberjs.com/u/machty)
#### Post date: [July 30, 2013, 8:57pm UTC](https://discuss.emberjs.com/t/should-i-use-setupcontroller-or-model-from-my-routes/1970/10 "2013-07-30T20:57:55Z")

</div>

@kingpin2k you are correct. Return from `model` will allow `modelFor` to return the correct value mid-transition.

---

<div class="post-metadata">

### Author: ![rmbrad](https://avatars.discourse-cdn.com/v4/letter/r/e9c0ed/32.png) [@rmbrad](https://discuss.emberjs.com/u/rmbrad)
#### Post date: [July 31, 2013, 2:45am UTC](https://discuss.emberjs.com/t/should-i-use-setupcontroller-or-model-from-my-routes/1970/11 "2013-07-31T02:45:35Z")

</div>

Whoops, yeah forget that case. I always implement my model hooks from the standpoint of what do I need to do to put the application into a state to render the requested resource starting with just the requested URL. This usually starts within the application route, returning promises along the way so I’m sure everything is ready when I need it. Then all the internal transitions, modelFor’s and linkTo’s just work.
