# Model hook does not get called for nested routes

**URL:** <https://discuss.emberjs.com/t/model-hook-does-not-get-called-for-nested-routes/7838>\
**Category:** Ember CLI\
**Created:** [April 21, 2015, 6:22pm UTC](https://discuss.emberjs.com/t/model-hook-does-not-get-called-for-nested-routes/7838 "2015-04-21T18:22:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kavitshah8](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/kavitshah8/32/15897_2.png) [@kavitshah8](https://discuss.emberjs.com/u/kavitshah8)\
**Post date:** [April 21, 2015, 6:22pm UTC](https://discuss.emberjs.com/t/model-hook-does-not-get-called-for-nested-routes/7838/1 "2015-04-21T18:22:47Z")

</div>

I am using ember-cli v 0.2.1. My `router.js` looks as follows:

```auto
Router.map(function() {
  this.route('projects', function() {
    this.route('project-id', { path:':id' });
  });

```

My `routes/projects.js` looks as follows:

```auto
export default Ember.Route.extend({
  model: function(){
    var clientID = 'FooBarFooBarFooBar';
    var query = 'motorcycle';
    var url = "https://api.behance.net/v2/projects?q="+ query +"&client_id="+ clientID;
    return ajax({url:url,type:'GET', dataType: "jsonp"}).then(function(res){	
      return res.projects;
    });
}
});

```

Part of my `templates/projects.hbs` looks as follows:

```auto
{{#each item in content}}
<div class="row">
<h4>
   {{#link-to 'projects.project-id' item}}
      {{item.name}}
   {{/link-to}}
</h4>
{{/each}}
{{outlet}}

```

My `routes/projects/project-id.js` looks as follows:

```auto
export default Ember.Route.extend({
	model: function(parmas){
	  var clientID = 'FooBarFooBar';
	  var url = "https://api.behance.net/v2/projects/"+ parmas.id +"?api_key="+ clientID;
	  debugger;
	  return ajax({url:url, type:'GET', dataType: "jsonp"}).then(function(res){	
	    debugger;	
	    return res.project;
           });
         },
});

```

When I click on a particular project, URL changes with appropriate project id but model hook in `project-id.js` does not get called. It gets called when I refresh the page with URL /projects/:project\_id already set in it. Please, help me understand this behaviour. I want the model hook to be called when someone clicks on the particular project.

---

<div class="post-metadata">

**Author:** ![kushdilip](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/kushdilip/32/10266_2.png) [@kushdilip](https://discuss.emberjs.com/u/kushdilip)\
**Post date:** [April 21, 2015, 7:11pm UTC](https://discuss.emberjs.com/t/model-hook-does-not-get-called-for-nested-routes/7838/2 "2015-04-21T19:11:49Z")

</div>

I have observed that `setupController` hook is called when you transition inside app and `model` and `setupController` hooks are called in order when you reload the route url. Try implementing `setupController` method and debug it both ways.

---

<div class="post-metadata">

**Author:** ![splattne](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/splattne/32/3694_2.png) [@splattne](https://discuss.emberjs.com/u/splattne)\
**Post date:** [April 21, 2015, 7:52pm UTC](https://discuss.emberjs.com/t/model-hook-does-not-get-called-for-nested-routes/7838/3 "2015-04-21T19:52:58Z")

</div>

```
{{#link-to 'projects.project-id' item}}

```

You could force the model hook call by changing the line above to

```
{{#link-to 'projects.project-id' item.id}}

```

---

<div class="post-metadata">

**Author:** ![kavitshah8](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/kavitshah8/32/15897_2.png) [@kavitshah8](https://discuss.emberjs.com/u/kavitshah8)\
**Post date:** [April 21, 2015, 9:59pm UTC](https://discuss.emberjs.com/t/model-hook-does-not-get-called-for-nested-routes/7838/4 "2015-04-21T21:59:22Z")

</div>

Passing item.id calls the model hook which satisfies the purpose for now. Thanks for the help.

---

<div class="post-metadata">

**Author:** ![herom](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/herom/32/14642_2.png) [@herom](https://discuss.emberjs.com/u/herom)\
**Post date:** [April 21, 2015, 10:11pm UTC](https://discuss.emberjs.com/t/model-hook-does-not-get-called-for-nested-routes/7838/5 "2015-04-21T22:11:47Z")

</div>

The behaviour you’re seeing is accused by the fact, that the `beforeModel` and `model` hooks are not invoked if you’re passing an object within the `{{link-to}}` helper. If you pass an object, this object will be treated like the already resolved `model` which causes Ember to bypass the all hooks before `afterModel`.

---

<div class="post-metadata">

**Author:** ![kavitshah8](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/kavitshah8/32/15897_2.png) [@kavitshah8](https://discuss.emberjs.com/u/kavitshah8)\
**Post date:** [April 22, 2015, 12:13am UTC](https://discuss.emberjs.com/t/model-hook-does-not-get-called-for-nested-routes/7838/7 "2015-04-22T00:13:49Z")

</div>

Yes, you are right. `setupController` hook gets invoked as well. Thanks.

---

<div class="post-metadata">

**Author:** ![kavitshah8](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/kavitshah8/32/15897_2.png) [@kavitshah8](https://discuss.emberjs.com/u/kavitshah8)\
**Post date:** [April 22, 2015, 9:26pm UTC](https://discuss.emberjs.com/t/model-hook-does-not-get-called-for-nested-routes/7838/8 "2015-04-22T21:26:43Z")

</div>

@herom @splattne @kushdilip I understand the behavior now. If I pass a `string (item.id)` inside `{{link-to}}` helper, model and beforeModel hook gets invoked, but if I pass an `object (item)` model and beforeModel hook does not get invoked. And setupController hook always gets invoked. Thanks.

---

<div class="post-metadata">

**Author:** ![toranb](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/toranb/32/16579_2.png) [@toranb](https://discuss.emberjs.com/u/toranb)\
**Post date:** [April 29, 2015, 2:35pm UTC](https://discuss.emberjs.com/t/model-hook-does-not-get-called-for-nested-routes/7838/9 "2015-04-29T14:35:50Z")

</div>

I also ran into this but found that when I link-to or transitionTo a parent route (with no params) I needed to tell the router w/ a query string to always refresh. If anyone is interested in how I did this - [this commit](https://github.com/toranb/route-always-fires-model-hook-example/commit/e4c6dffa6609ba081aec99df49612d031d57f001) shows all the changes required to force my top level model hook to fire any time you link-to or transitionTo it
