Search code examples
javascriptember.jsember-routerember-controllers

this.controller is undefined in Route class


I am developing SPA using EmberJS latest 1.7.0, according to Ember docs, the route contains a property that holds a reference to the controller, but it always returns undefined in all my routes.

ApplicationRoute = Ember.Route.extend({
  queryParams: {
      tsk: {
          // Opt into full transition
          refreshModel: true
      }
  },
  model: function(params) {
      var task = {"name" : "task"};

      this.controller.set('currentTask',task);
      return task;
  }
});

and also in another route

SubfileRoute = Ember.Route.extend({
  model : function(params) {

      console.log(this.controllerName);
      console.log(this.controller);

  }
});

both console.log(this.controllerName); and console.log(this.controller); are undefined,

how to properly work with ember controllers? only way that seems to be working is through this.controllerFor()

Update:

I also tried to use this method:

needs:'application',
currentTask : Ember.computed.alias('controllers.application.currentTask'),

but also the currentTask is always undefined, so as the the controller it self.


Solution

  • If you want to set properties on the controller, you should use the setupController hook:

    model: function(params) {
      return {"name" : "task"};
    },
    
    setupController: function(controller, task) {
      controller.set('currentTask',task);
    }