Vlad Goran
Vlad Goran

Reputation: 503

Emberjs Router: view property is not defined on associated controller

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.

Upvotes: 0

Views: 1491

Answers (2)

ghempton
ghempton

Reputation: 7947

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

Upvotes: 3

Mike Aski
Mike Aski

Reputation: 9236

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.

Upvotes: 2

Related Questions