0

Having two routes (comments, trackbacks) nested within post I try to access content of commentsController from trackbacksController after entering the App directly through /posts/1/trackbacks. Unfortunately it seems like the controller is not fully initialized and the content doesn't get loaded.

This is how the Router looks like:

Router = Ember.Router.create
  root: Ember.Route.extend
    index: Ember.Route.extend
      route: '/'

    post: Ember.Route.extend
      route: '/posts/:post_id'

      index: Ember.Route.extend
        route: '/'
        redirectsTo: 'comments'

      comments: Ember.Route.extend
        route: '/comments'
        connectOutlets: (router) ->
          controller = router.get('postController')
          controller.connectOutlet 'comments', controller.get('comments')

      trackbacks: Ember.Route.extend
        route: '/trackbacks'
        connectOutlets: (router) ->
          controller = router.get('postController')
          controller.connectOutlet 'trackbacks', controller.get('trackbacks')

And here is the TrackbacksController:

App.TrackbacksController = App.ArrayController.extend
  init: ->
    console.log App.router.get('attributesController.content') # : []
    @_super()

Is there a best practice to initialize router.commentsController manually to get it's content from trackbacksController? Is there anything wrong concerning my approach?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
pex
  • 7,351
  • 4
  • 32
  • 41

1 Answers1

4

You can access all controllers from a controller, using its controllers property. For example, from trackbacksController, you can use this.get('controllers.commentsController')

EDIT: I realize I did'nt get your problem. I think there is some dependencies between the two controllers, and from my point of view, you can't rely on the application initialization order. So in the init method, you will not be able to access other controllers.

But I think you can put an observer in the trackbacksController, which observes controllers.commentsController.content or controller.commentsController.@each, so when the commentsController is populated, you will be notified.

sly7_7
  • 11,961
  • 3
  • 40
  • 54
  • Thank you - I am doing exactly that in a property and for some reason it doesn't get triggered. For me it seems like the controller content doesn't get assigned until I call the specific route. – pex Aug 10 '12 at 09:29
  • ... I might try it with an observer, tough. – pex Aug 10 '12 at 09:29
  • In fact, the controller content is assigned when you assign it, either explicitly by controller.set('content', ...), or when you call connectOutlet() with a context object. – sly7_7 Aug 10 '12 at 09:43