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

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?

1 Like

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

What a Timing!

I made a small test but it not seems to work: http://jsbin.com/odiwaj/1/edit

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'
});

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 http://jsbin.com/odiwaj/4/edit

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

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

https://github.com/emberjs/ember.js/blob/ba88186b01444de21f9a3ec8b5a850705f4b1cb8/packages/ember-routing/lib/system/route.js#L850-L858

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.

https://github.com/emberjs/ember.js/pull/2983

1 Like

Many Thanks for the affort @SelvaG