简体   繁体   English

Ember是否应该在link-to / transitionTo上触发所有父路由挂钩?

[英]Is Ember supposed to fire all parent route hooks on link-to/transitionTo?

I'm having a hard time understanding how the Ember router calls hooks on nested resources during link-to transitions. 我很难理解Ember路由器在链接到转换期间如何调用嵌套资源上的挂钩。 There are no dynamic segments involved in this example. 此示例中没有涉及动态细分。

Here's a contrived example that mimics my app: 这是一个模仿我的应用程序的人为例子:

App.Router.map(function() {
  //Modal resources
  this.resource('modal', function(){

    this.resource('posts', function(){
      this.route('new');
      this.route('edit')
    });

    this.resource('photos', function(){
      this.route('new');
      this.route('edit');
    });
})

On a FULL PAGE RELOAD of the posts route, let's say, we see that the beforeModel and afterModel hooks on Application > Index > Modal routes are fired in sequence. 在帖子路由的完整页面RELOAD上,让我们说,我们看到依次触发Application> Index> Modal路由上的beforeModel和afterModel挂钩。 Good, makes sense. 好,有道理。

Attempting URL transition to /modal/posts ember.js?body=1:3912
Transition #0: application: calling beforeModel hook ember.js?body=1:3912
Transition #0: application: calling deserialize hook ember.js?body=1:3912
Transition #0: application: calling afterModel hook ember.js?body=1:3912
Transition #0: modal: calling beforeModel hook ember.js?body=1:3912
Transition #0: modal: calling deserialize hook ember.js?body=1:3912
Transition #0: modal: calling afterModel hook ember.js?body=1:3912
Transition #0: posts: calling beforeModel hook ember.js?body=1:3912
Transition #0: posts: calling deserialize hook ember.js?body=1:3912
Transition #0: posts: calling afterModel hook ember.js?body=1:3912
Transitioned into 'posts' ember.js?body=1:3912
Transition #0: TRANSITION COMPLETE. 

However, when I click 但是,当我点击

{{link-to 'Go to Photos' 'photos'}}

in EITHER the modal.hbs template or the posts.hbs template, it doesn't cascade through the Application > Index > Modal hook sequence. 在使用modal.hbs模板或posts.hbs模板时,它不会通过Application> Index> Modal钩子序列级联。 It only hits the very-most child leaf hooks: 它只击中最极端的儿童叶钩:

Attempting transition to photos ember.js?body=1:3912
Transition #1: photos: calling beforeModel hook ember.js?body=1:3912
Transition #1: photos: calling deserialize hook ember.js?body=1:3912
Transition #1: photos: calling afterModel hook ember.js?body=1:3912
Transition #1: Resolved all models on destination route; finalizing transition. 
Transitioned into 'photos' ember.js?body=1:3912
Transition #1: TRANSITION COMPLETE. 

Could some kind soul clear up how the router is supposed to work, especially on link-to transitions? 有些灵魂可以清楚路由器应该如何工作,特别是在链接到转换时? I want all parent routes' beforeModel/afterModel hooks to be hit in sequence. 我希望所有父路由的beforeModel / afterModel挂钩按顺序命中。 From what I've researched (a lot), Ember is supposed to hit all the parent route beforeModel/afterModel hooks, top-down, on every transition. 根据我的研究(很多),Ember应该在每次转换之前,在模型/ afterModel挂钩,自上而下之前击中所有父路线。


Edit specifically for kingpin2K's response: Ok that makes sense, but 专门针对kingpin2K的回复进行编辑:好的,但是

App.Modal = Ember.Route.extend({

  model: function(params){
    //load some data that you want modal.photos to also have access to
    console.log('Modal route model hit')
  },

  afterModel: function(model) {
    this.transitionTo('modal.photos')
  }
});

calls the Modal parent route twice when navigating to it. 导航到它时,调用Modal父路由两次。 1) on the direct hit to Modal url, and 2) again on the afterModel redirect (transitionTo modal.photos). 1)直接命中Modal url,2)再次onModel重定向(transitionTo modal.photos)。 Under your explanation, the Modal parent route hooks (especially the model hook!) wouldn't fire on the subsequent transition from modal (parent) to modal.photos (child), but they are. 根据你的解释,模态父路径挂钩(特别是模型挂钩!)不会在从模态(父)到modal.photos(子)的后续转换中触发,但它们是。 You will get two console logs when you go to the parent 'modal' route. 当您转到父“模态”路线时,您将获得两个控制台日志。

Thanks again for your help. 再次感谢你的帮助。

It's easiest to think of the router as a stack, with each resource/route an item in the stack. 最简单的方法是将路由器视为堆栈,每个资源/路由都是堆栈中的项目。 As you move from resource to resource it pops irrelevant items from the stack, then pushes new relevant items that aren't yet fetched. 当您从资源移动到资源时,它会从堆栈中弹出不相关的项目,然后推送尚未获取的新相关项目。

A parent resource shouldn't be dependent on the child resource, or it wouldn't really be a parent resource. 父资源不应该依赖于子资源,或者它实际上不是父资源。 With this paradigm in mind, it would be pointless to refetch parent models when transitioning to a new resource/route in the router. 考虑到这种范例,在转换到路由器中的新资源/路由时重新获取父模型是没有意义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM