0

I'm trying to move my app to the new Emberjs Router component that uses "outlets".

See refs: http://emberjs.com/guides/outlets/ and this: http://codebrief.com/2012/07/anatomy-of-an-ember-dot-js-app-part-i-redux-routing-and-outlets/

How I understand this to work: When you connect an outlet using a string the following things happen

  1. Ember looks for the view definition on the App and instantiates it
  2. Ember looks for the controller definition on the App and creates a instance of it
  3. Hooks the two between each other like so: sets views controller property and sets controllers view property

--- Works well up until the last step. I can't seem to get the view property to set on the controller.

JSBin here: http://jsbin.com/ekekir/40/edit

Relevant code:

App.Router

App.Router = Ember.Router.extend({
  root: Ember.Route.extend({
    state1: Ember.Route.extend({
      route: '/state1',

      connectOutlets: function (router) {
        router.get('applicationController').connectOutlet('state1')
      }
    })
  })
});

Controller and View

App.State1View = Ember.View.extend ({ 
  templateName: 'state1',
  submit: function () {
    this.get('controller').doLogView();
    return false;
  }
});

App.State1Controller = Ember.Controller.extend({
  doLogView: function () {
    console.log('Getting controller view:');
    console.log(this.get('view'));
  }
});

At the end it returns a big fat null.

Am I doing something wrong or is this just the way it should be?

BTW: This is using ember-1.0.pre.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Vlad Goran
  • 463
  • 4
  • 11

2 Answers2

3

Mike Aski is correct that the controller should not know about the view.

However, the current implementation actually does set the view on a controller, but not the controller you would expect. In your example above, the view property of applicationController will be set to an instance of State1View.

Relevant code from Ember's source here:

https://github.com/emberjs/ember.js/blob/master/packages/ember-views/lib/system/controller.js#L140

ghempton
  • 7,777
  • 7
  • 48
  • 53
  • Thank you for the clarification. What Mike Aski is saying makes sense and you should't try to do this but if you *need* to you can do this: `App.get('router.applicationController.view')` – Vlad Goran Sep 13 '12 at 11:46
2

It is a normal behavior, as the controller does not have to know the view rendering its content. You would have a dependency cycle (and a broken design...).

The dependencies follow the chain: V -> C -> M

Moreover, the controller is injected once, and the view is created each time it is displayed.

Mike Aski
  • 9,180
  • 4
  • 46
  • 63
  • So this statement "Set the view property of the controller to the newly instantiated view." in [here](http://codebrief.com/2012/07/anatomy-of-an-ember-dot-js-app-part-i-redux-routing-and-outlets/) is just plain wrong?Or is it referring to the ApplicationController? – Vlad Goran Sep 11 '12 at 14:09
  • 2
    @oportocala as you can see here: https://github.com/emberjs/ember.js/blob/master/packages/ember-views/lib/system/controller.js#L139, `connectOutlets` sets the controller as the view's context, but does not sets the view as a controller's property. – sly7_7 Sep 12 '12 at 14:02