Skip to content Skip to sidebar Skip to footer

Why ApplicationRoute Doesn't React To Transitions

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 th

Solution 1:

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.

application
    index
    photos
        view
        new

A pretty simple set of routes. Now let's suppose that you wanted to visit the photos.view route. Ember would follow these steps:

  1. Run the application route hooks. (Including beforeModel, model, and afterModel).
  2. Run the photos route hooks. (Including beforeModel, model, and afterModel).
  3. Run the view route hooks. (Including beforeModel, model, and afterModel).

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 to photos.new. Ember isn't going to re-run the application and photos 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 the photos.new setup hooks. If you transitioned to the index 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.


Post a Comment for "Why ApplicationRoute Doesn't React To Transitions"