When answering other question I made incorrect statement writing that ApplicationRoute.beforeModel()
hook is ran after every transition. When made aware of this fact I confirmed that the hook is ran only once using barebone Ember app.
Unfortunately, there was nothing I could found in documentation that would explain this behaviour. First paragraph of beforeModel documentation states:
This hook is the first of the route entry validation hooks called when an attempt is made to transition into a route or one of its children.
As for ApplicationRoute
- there is not a lot about it, in action bubbling part of the guide we can find information that action bubbles from controller through route to its parent and it lists ApplicationRoute
as a top parent of routes:
If neither the template's controller nor the currently active route implements a handler, the action will continue to bubble to any parent routes. Ultimately, if an ApplicationRoute is defined, it will have an opportunity to handle the action.
Logically, this should mean, that every transition should run ApplicationRoute
hook which is in contrary to what actually happens.
So the question is:
Why ApplicationRoute
doesn't answer to transition events and what are the other differences from defined Routes?
It seems like your main questions is: why doesn't the application route run with every transition? The long answer is a bit complicated, but the short answer is: because it doesn't have to.
For the long answer, let's make an example route hierarchy.
A pretty simple set of routes. Now let's suppose that you wanted to visit the
photos.view
route. Ember would follow these steps:application
route hooks. (IncludingbeforeModel
,model
, andafterModel
).photos
route hooks. (IncludingbeforeModel
,model
, andafterModel
).view
route hooks. (IncludingbeforeModel
,model
, andafterModel
).Ember has to initialize the route for every parent route of the route you want to visit. That makes sense. But let's say you transitioned from
photos.view
tophotos.new
. Ember isn't going to re-run theapplication
andphotos
route setup hooks. It doesn't need to. Those models have already been resolved and nothing has invalidated them. Ember is only going to run thephotos.new
setup hooks. If you transitioned to theindex
route, it would only run setup hooks for that route, not the application route.Short story long, Ember isn't go to re-run setup hooks and model fetching logic if it doesn't have to. Unless you invalidate some cached data, or force a reload, Ember is only going to run your
application
route hooks once.If you want logic that runs before every transition, something that I've done before is to create a base route that all of my routes extend from, then override the
activate
hook.