2

If I have an application using the Router architecture, how do I get the controller of the parent route?

suppose a route like

Router: Ember.Router.extend 
    enableLogging: true
    root: Ember.Route.extend
        main: Ember.Route.extend
            route: '/'
            connectOutlets: (router, event) ->
                 router.get('applicationController').connectOutlet('main')
            editor: Ember.Route.extend
                route: '/editor'
                connectOutlets: (router, event) ->
                    router.get('mainController').connectOutlet('editor')

So how would my EditorController get a hold of my MainController?

I've tried @get('parentController'), @get('parent'), and @get('controller') with no success

sly7_7
  • 11,961
  • 3
  • 40
  • 54
wmarbut
  • 4,595
  • 7
  • 42
  • 72

2 Answers2

6

You can access the router through the target property of a controller.

e.g, in your case, from any controller:

@get('target.mainController')

Or, if you do not use head revision of Ember:

@getPath('target.mainController')
Mike Aski
  • 9,180
  • 4
  • 46
  • 63
  • Thanks! Looks like it is actually `@getPath('taget.mainController')`, as `@get('target.mainController')` returns undefined. I'll look to see if I'm doing anything wonky, could you also try to confirm? I'd like to make sure this is 100% right before I accept. – wmarbut Jul 26 '12 at 20:31
  • Absolutely, I apologize for puzzling you. Using path expressions with `get` is something which appeared very recently in latest revisions. Unless you are tracking head, you should indeed use `getPath`. Updated answer. – Mike Aski Jul 26 '12 at 21:03
2

According to my understanding, an application is supposed to have only one router, so why dont we just do:

@get('App.router.mainController')

I have no idea on what is the right approach! I feel like 'target' is storing the object to which actions or events are dispatched.

sabithpocker
  • 15,274
  • 1
  • 42
  • 75
  • Thanks, I can confirm this also works! @Mike Aski got it first so he keeps the answer, but I'll give an upvote! :) – wmarbut Jul 27 '12 at 02:15