Search code examples
serviceember-cli

Ember. Inject an addon service into additional 'areas'


How would I inject an addon service into other 'places'?

For example, if I install an addon that injects into controllers & components with the code below:

export default {
  name: 'notification-messages-service',
    initialize() {
    let application = arguments[1] || arguments[0];
    application.register('notification-messages:service', NotificationMessagesService);

     ['controller', 'component'].forEach(injectionTarget => {
        application.inject(injectionTarget, 'notifications', 'notification-messages:service');
    });
}};

How would I then inject the same service (the same singleton) into services & routes - my requirement is actually inject into a single service, services:messages?

I don't believe I can use

notifications: Ember.inject.service();

because in the addon the service is written as:

export default Ember.ArrayProxy.extend({...});

I can change the addon, of course, but my changes would be gone once the addon is updated.

Thanks for looking, N


Solution

  • I was able to inject the addon to my specific service by creating a new initialiser with the code below:

    export default {
    name: 'inject-ember-notification-service',
    
    initialize: function(container, app) {
        app.inject('services:message', 'notifications', 'notification-messages:service');
    }
    };
    

    Fiendishly obtuse, ember!

    Thanks to you all for your input.