Why ApplicationRoute doesn't react to transiti

2019-09-08 08:05发布

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?

1条回答
贼婆χ
2楼-- · 2019-09-08 08:51

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.

查看更多
登录 后发表回答