J__
J__

Reputation: 635

Injecting application controller into all other controllers

I'm hoping to use my application controller for displaying dynamic modal components. It would be convenient if I could inject the application controller into all other controllers, rather than explicitly injecting it in each controller file where it's needed.

initialize = (app) ->

   app.inject 'controller', 'appController', 'controller:application'
   app.inject 'component' , 'appController', 'controller:application'

InjectApplicationController =
  name: 'inject-application-controller'
  initialize: initialize

`export {initialize}`
`export default InjectApplicationController`

please forgive the coffeescript; this gives me an error, that a controller cannot be injected into other controllers. Is there a workaround?

Upvotes: 1

Views: 339

Answers (1)

Lux
Lux

Reputation: 18240

I strongly recommend to use an service to hold the state, but you can just reopen Ember.Controller in the initializer:

Ember.Controller.reopen({
  appController: Ember.controller.inject('application')
});

Upvotes: 3

Related Questions