Dependancy Injection in initializers confusing (registry/container?)

I use an initializer to inject the currently logged in user into all controllers (jsfiddle)

Now, initializers have been split up into initializers and instance-initializers.

So I’m trying to move my initializer to an instance-intiailizer. But questions come to mind:

  1. registry is mentioned in the above link, but nowhere else.

    • What is it?
    • How does it differ from container?
  2. instance is a parameter for an instanceInitializer. But an instance of what? It doesn’t seem to be an instance of the application which is what I would have expected.

  3. I used to use application.inject as per the documentation. However, when used inside an instance-initiailizer I get application.inject is not a function

    • Does this mean we should use container.injection instead?
    • If so, isn’t that meant to be private?
  4. As I understand it (and I may be wrong) Everything must have been registered and injected before the instanceInitializers get executed. But this removes the ability to look something up, and then register and inject based upon that lookup. (Which means the pattern in the above jsfiddle is no longer possible)

Could anybody in-the-know please clarify.

I can’t answer your questions but this seems like it would be worth creating an issue on github. Even if it’s not a bug it should at least be better documented.

I did encounter registry on a recent ember code dive. Apparently there was a major refactor to decouple container and registry.

@varblob Good idea

I’m currently dealing with the same issue (currentUser injected into controllers/routes). I’d love to hear if anyone comes up with anything.

I solved it for now like this:

SessionService = Ember.Service.extend({
    store: Ember.inject.service(),
    user: Ember.computed(function() {
        return this.get('store').all('user').filterBy('isLoggedIn').popObject();

And then use this.get('session.user') instead of this.get('currentUser')

I don’t know why this works compared to just injecting the user, but I actually prefer the solution.

Having the same issue as @amk and I remembered this article:

According to that post you can still do injection in your instance initializer like so:

export default {
  name: "whatever",
  initialize: function(application) {
    const { registry } = application;
    const utility = Ember.Object.create();
    registry.register('service:some-service', utility, { instantiate: false, singleton: true });
    registry.injection('route', 'someService', 'service:current-user');

Also if you are using Ember-CLI make sure to use required naming convention: