首页 > 解决方案 > EmberJS:强制转换到父路由

问题描述

我有两条路线:一条名为 的主路线parent和一条名为 的子路线parent.child。当在 中发生某些事情(我们称之为X)时parent.child,我想过渡到parent,但由于技术上我们已经在那里,Ember 什么也不做。

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

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

标签: javascriptember.js

解决方案


你可以调用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');
  }
}

推荐阅读