Creating a helper method which accesses another controller for certain controllers

So, I have a notification-container controller, which implements a ‘pushNotification’ action:

app/contolllers/notification-container.js:

export default Ember.Controller.extend({
  actions: {
    pushNotification: function (type, title, message) {
      // send a notification....
    }
  }
});

If I would like to call that method from another controller, I have to tell the controller that it needs the other controller:

needs: ['notification-container'],

and then in order to trigger the notification:

this.get('controllers.notification-container').send('pushNotification', 'success', 'title', 'test');

That not only doesn’t look very nice, but I have to tell every controller that needs to send notifications that it needs to depend on the notification-container controller. So, I was hoping to find a smarter way to deal with this. Unfortunately it looks like I can’t use mixins easily if I need to call another controller. So, the only thing that seems to work is to extend all the different ember controller types with custom ones, that implement a function to send out notifications. Still a lot of duplicate code…

I found there is an ControllerMixin, however besides its name, it does not seem to be made for being a mixing for your controllers.

Is there any good way to deal with that? In the end I would just like to do something like “Notification.warn(‘title’, ‘text’);” which then would make the appropriate calls to the controller, but I can’t figure out how to make that available globally.

The action should be on a route, probably application route. That way you can send the action from anywhere

1 Like

Hmm, that method actually is not relevant to any routing, so I figured the controller would be the appropriate place for that. Is there any rule for what should go in the routes and what in the controllers? I actually didn’t want to put this method in the application controller, as I try to keep this as pluggable as possible. But that probably is an entire issue on its own.

I have a jsbin, which shows the whole thing here: http://emberjs.jsbin.com/decojele/2/edit, which in itself is mostly stolen from Aaron Haurwitz

If putting that method into the application route, can I then access it from a mixing?