简体   繁体   English

EmberJS:强制转换到父路由

[英]EmberJS: force transition to parent route

I've got two routes: a main route, called parent , and a child route of it called parent.child .我有两条路线:一条名为parent的主路线和一条名为parent.child的子路线。 When something happens (let's call it X ) in parent.child , I want to transition to parent , but since technically we're already there, Ember does nothing.当在parent.child发生某些事情时(我们称之为X ),我想转换到parent ,但由于技术上我们已经在那里,Ember 什么都不做。

// 'parent.child' controller
this.transitionToRoute('parent');

So I want to know if there's a way to force this "transition".所以我想知道是否有办法强制这种“过渡”。 The reason is that there's code in parent that needs to re-run after X occurs.原因是parent中有代码需要在X发生后重新运行。

You can call refresh on your parent route.您可以在父路由上调用refresh

Now the simplest way to call an action on your parent route is, well, to define one, and then use send on your controller to let the action bubble up.现在在你的父路由上调用一个动作的最简单的方法是定义一个,然后在你的控制器上使用send来让动作冒泡。

So your parent route:所以你的父路线:

class ParentRoute extends Route {
  @action
  refreshParent() { // dont name it refresh because of the naming conflict
    this.refresh();
  }
}

and your child controller:和您的子控制器:

class ChildController extends Controller {
  @action
  refreshParentRoute() { // again, another name
    // this will first look for a `refreshParent` action on this controller,
    // then the `child` route and last the `parent` route.
    this.send('refreshParent');
  }
}

In order to get back up our of the child route you can call为了恢复我们的子路线,您可以致电

this.transitionToRoute('parent.index');

I'm not sure this solution will fix your problem without seeing your app, for example this probably won't re-run hooks in the parent route, you may need to either move them into routes/parent/index or re-design how those hooks work.我不确定这个解决方案会在没有看到你的应用程序的情况下解决你的问题,例如这可能不会在父路由中重新运行钩子,你可能需要将它们移动到routes/parent/index或重新设计如何那些钩子起作用了。

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

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