Reputation: 1335
How to specify which controller to use in a route in ember 2.0? Default is that each route has its own controller and if I don't have code for a controller then a default empty controller is generated at runtime.
I want to specify that several routes share the same controller (and model). I see that in the documentation to an older version of ember it is possible to specify the controller with the "controllerName" property in the routes definition but that part of the documentation has been removed for the lastest ember version. And when I test the controllerName property it doesn't work.
Upvotes: 1
Views: 361
Reputation: 1335
I figured it out. It turns out controllerName
does work after all.
I just did this:
// route/home/books/book/details
import Ember from 'ember';
export default Ember.Route.extend({
controllerName: "home/books/book/index",
});
Then book
and book/details
shares controller. They already share model because the details
route is a sub-route of book
. So it works nicely.
Upvotes: 2
Reputation: 9280
It's not exactly what you are looking for but you could extend the controller that you want to use on the other routes.
import FooIndexController from 'ember-app/foo/index/controller';
export default FooIndexController.extend({});
You may also consider a mixin if you have a lot of code that should be shared by many controllers. It can be a much cleaner solution.
import Ember from 'ember';
import BaseController from 'ember-app/mixins/base-controller';
export default Ember.Controller.extend(BaseController, {
// Code specific to _this_ controller lives here
});
Upvotes: 4