接听时other question我做了错误的陈述,写的是 ApplicationRoute.beforeModel()
钩子(Hook)在每次转换后运行。当意识到这一事实时,我确认该钩子(Hook)仅使用准系统 Ember 应用程序运行一次。
不幸的是,我在文档中找不到任何可以解释这种行为的内容。 First paragraph of beforeModel documentation状态:
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
- 没有太多相关内容,在 action bubbling 中在指南的一部分中,我们可以找到有关操作从 Controller 通过路由冒泡到其父级的信息,并将 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
Hook ,这与实际发生的情况相反。
所以问题是:
为什么 ApplicationRoute
不响应转换事件以及与已定义路由的其他区别是什么?
最佳答案
您的主要问题似乎是:为什么应用程序路由不随每次转换一起运行?长的答案有点复杂,但简短的答案是:因为它不必这样做。
对于长答案,让我们制作一个示例路由层次结构。
application
index
photos
view
new
一组非常简单的路线。现在假设您想要访问 photos.view
路线。 Ember 将遵循以下步骤:
- 运行
application
路由 Hook 。 (包括beforeModel
、model
和afterModel
)。 - 运行
photos
路由 Hook 。 (包括beforeModel
、model
和afterModel
)。 - 运行
view
路由 Hook 。 (包括beforeModel
、model
和afterModel
)。
Ember 必须为您要访问的路线的每个父路线初始化路线。这就说得通了。但假设您从 photos.view
转换为 photos.new
。 Ember 不会重新运行应用程序
和照片
路由设置 Hook 。不需要。这些模型已经得到解决,没有任何东西可以使它们失效。 Ember 只会运行 photos.new
设置 Hook 。如果您转换到 index
路由,它只会运行该路由的设置 Hook ,而不是应用程序路由。
长话短说,如果没有必要,Ember 不会重新运行设置 Hook 和模型获取逻辑。除非您使某些缓存数据无效或强制重新加载,否则 Ember 只会运行您的应用程序
路由 Hook 一次。
如果您希望逻辑在每个转换之前运行,我之前所做的就是创建一个所有路由都从中扩展的基本路由,然后覆盖 activate
hook .
关于javascript - 为什么 ApplicationRoute 对转换没有反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32331491/