EmberJS:强制过渡到父路由



>我有两条路线:一条名为parent的主路线,以及一条名为parent.child的子路线。当parent.child发生某些事情(我们称之为X(时,我想过渡到parent,但由于从技术上讲我们已经在那里,余烬什么都不做。

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

所以我想知道是否有办法强制这种"过渡"。原因是parent中的代码需要在X发生后重新运行。

您可以在父路由上呼叫refresh

现在,在父路由上调用操作的最简单方法是定义一个操作,然后在控制器上使用send让操作冒泡。

所以你的父路线:

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

和你的孩子控制器:

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');
}
}

为了恢复我们的子路线,您可以致电

this.transitionToRoute('parent.index');

我不确定此解决方案是否会在不看到您的应用程序的情况下解决您的问题,例如,这可能不会在父路由中重新运行钩子,您可能需要将它们移动到routes/parent/index或重新设计这些钩子的工作方式。

最新更新