Is it possible for many routes to share the same controller?


#1

Is it possible for many routes to share the same controller without creating there own?

(one use case will be a signup form with many stages)

as answered in similar question:

I know that I can do this:

App.LocationsEditRoute = Ember.Route.extend({

setupController: function(controller, model) {
    this.controllerFor('locations.new').setProperties({isNew:false,content:model});
},
renderTemplate: function() {
    this.render('locations/new')         
}
});

but it seems that App.LocationsEditController is exist in the memory anyway, and just replace by another later. it it possible to prevent its creation in the first place?


#2

I haven’t tried this out but it looks like you can now define what controller a route uses via controllerName

From the RC7 release notes:

Routes can now specify their controller class name with controllerName


#3

What a Timing!

I made a small test but it not seems to work:

maybe I’m not using it right…

App.Router.map(function () {
  this.resource('signup', { path : '' } , function () {
    this.route('step1', { path : 'step1' });
    this.route('step2', { path : 'step2' });
  });
});

App.SignupStep1Route = Ember.Route.extend({
  controllerName : 'SignupStep2Controller'
});

App.SignupStep1Controller = Ember.ObjectController.extend({
  test : 'test 1'
});

App.SignupStep2Controller = Ember.ObjectController.extend({
  test : 'test 2'
});

#4

First to see your jsbin in action, you need to have an application template with an {{outlet}} to act as a placeholder for other templates.

Updated jsbin

But there seems to be some tweaks with the Route#controllerName which breaks it. Will have a deeper look at the code once


#5

After going through the code part, found that there is something missing in the following lines

while setting the controller context to the template.

On searching on the emberjs commits and issues, found that the following pull request has not been merged yet which is the root cause of the issue.


#6

Many Thanks for the affort @SelvaG