为什么ApplicationRoute不对转换做出反应 [英] Why ApplicationRoute doesn't react to transitions

查看:144
本文介绍了为什么ApplicationRoute不对转换做出反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当回答其他问题时,我做错了每次转换后都会运行 ApplicationRoute.beforeModel()钩子的语句。当知道这个事实时,我确认钩子只运行一次使用准系统Ember应用程序。

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.

不幸的是,在文档中没有找到任何可以解释这种行为的东西。 beforeModel文档的第一段指出:

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.

对于 ApplicationRoute - 没有太多关于它,在动作冒泡部分指南,我们可以找到从控制器通过路由到其动作泡沫的信息父母,并列出 ApplicationRoute 作为路线的顶级父母:

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:


如果模板的控制器也不是当前活动的路由实现一个处理程序,该操作将继续向任何父路由冒泡。最终,如果定义了一个ApplicationRoute,它将有机会处理该操作。

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.

逻辑上,这应该意味着,每个转换应该运行 ApplicationRoute 钩子,这与实际发生的相反。

Logically, this should mean, that every transition should run ApplicationRoute hook which is in contrary to what actually happens.

所以问题是:

为什么 ApplicationRoute 不回答转换事件,以及与定义的路由有什么不同?

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.

对于长的答案,让我们举一个例子路由层次结构。 / p>

For the long answer, let's make an example route hierarchy.

application
    index
    photos
        view
        new

一组非常简单的路线。现在我们假设你想访问 photos.view 路由。 Ember将遵循以下步骤:

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. 运行应用程序路由挂接。 (包括 beforeModel 模型 afterModel )。 / li>
  2. 运行照片路由挂接。 (包括 beforeModel 模型 afterModel )。 / li>
  3. 运行视图路由钩。 (包括 beforeModel 模型 afterModel )。 / li>
  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必须初始化您要访问的路线的每个父路由的路由。这就说得通了。但是让我们说你从 photos.view 转换到 photos.new 。 Ember 不会重新运行应用程序照片路由设置挂钩。它不需要。这些模型已经解决了,没有任何东西使它们失效。 Ember只能运行 photos.new 安装钩子。如果您转换到索引路由,则只会为该路由运行安装钩子,而不是应用程序路由。

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.

短篇小说,Ember不去重新运行安装钩子和模型提取逻辑,如果不需要。除非您使某些缓存数据无效或强制重新加载,否则Ember只会运行应用程序路由钩一次。

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.

这篇关于为什么ApplicationRoute不对转换做出反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆