Mudasi Ali のリードに基づいて、Ember v1.5 ソースを確認すると、次を使用します。
Coffeescript (JS/ES が必要な場合は、coffeescript.org を使用してトランスパイルします):
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')
上記は、すべてのルートと 2 つの便利な関数およびにプロパティ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)