。org进行transpile如果你想JS/ES):
Ember.Route.reopen
parentRoute: Em.computed ->
r = @router.router
if r.currentTransition
handlerInfos = r.currentTransition.state.handlerInfos
else
handlerInfos = r.state.handlerInfos
handlerInfos = this.router.router.state.handlerInfos
return unless handlerInfos
parent = @
for info in handlerInfos
break if info.handler == @
parent = info.handler
parent
parentRouteName: Em.computed.alias('parentRoute.routeName')
parentController: ->
@controllerFor @get('parentRouteName')
parentModel: ->
@modelFor @get('parentRouteName')
以上提供的属性parentRoute
和parentRouteName
,在所有的路线和两个方便的功能parentController()
和parentModel()
分别返回父控制器和模型,可能是有用的对于很多情况,特别是如果您表示将资源编辑为嵌套路线。
您还可以定义一些动作的意见/控制器等,以用于取消/返回处理如下:
Ember.Route.reopen
actions:
goBack: ->
@transitionTo @get('parentRouteName')
如果你有一个深刻的路由层次和想说的话跳过中间的路线,你只需要重写GoBack的如下:
App.SomeIntermediateRouteToSkipOnBack = Em.Route.extend
actions:
goBack: ->
# skip the immediate parent and use the grandparent route
@transitionTo @get('parentRoute.parentRouteName)
很好的答案 - 谢谢。我已经实现了这一点,它的作品是一种享受! – iHiD 2013-04-08 13:47:57
请注意,由于currentHandlerInfos可能未设置,因此您无法使用它来获取当前正在转换到的路由的父路由(例如,当正在加载页面时)。我试图在一个路由处理器基类中使用它来获取父类的模型而不知道它的名字,不幸的是页面加载太快了。 – 2013-07-04 06:35:19
因此,这是获取CURRENT路由的父路由名称的好方法,而不是从中调用该方法的路由对象。那是因为你通过'router'获取了路由的层次结构。 – Huafu 2014-05-11 08:33:29