Aaron Renoir
Aaron Renoir

Reputation: 4381

Injecting controllers into controllers on initialization no longer working (canary)

I just upgraded to the latest canary version of ember and noticed my initializer that injected the currentUser controller into all controllers no longer works.

Here is the code.

Ember.Application.initializer({
  name: "fetchUsers",
  after: "store",

  initialize: function(container, application) {

    var store, controller;

    application.deferReadiness();

    store      = container.lookup('store:main');
    controller = container.lookup('controller:currentUser');

    return store.find('user').then( function(users) {
      var currentUser;

      currentUser = users.findBy('isCurrent', true);

      controller.set('content', currentUser);

      application.inject('controller', 'currentUser', 'controller:currentUser');

      application.advanceReadiness();
   });
  }
});

This works fine in the release and beta branches but in canary I get the following error.

Error: Cannot inject a `controller:current-user` on other controller(s). Register the `controller:current-user` as a different type and perform the typeInjection.

How should I go about fixing this? I would like currentUser to be a ObjectController, is this possible?

Upvotes: 4

Views: 525

Answers (1)

Aaron Renoir
Aaron Renoir

Reputation: 4381

Okay this is my current initializer. I really just changed the currentUser ObjectController to an ObjectProxy. My only hang up now is where to store the CurrentUserObjectProxy file. I am leaning toward the models dir.

Ember.Application.initializer({
  name: "fetchUsers",
  after: "store",

  initialize: function(container, application) {

    var store, user, proxy;

    application.deferReadiness();

    store = container.lookup('store:main');

    proxy = App.CurrentUserObjectProxy.extend();

    container.register('user:current', proxy, {singleton: true});

    proxy = container.lookup('user:current');

    store.find('user').then( function(users) {

      user = users.findBy('isCurrent', true);

      proxy.set('content', user);

      application.inject('controller', 'currentUser', 'user:current');

      application.advanceReadiness();
    });
  }
});

I hope this helps someone.

Upvotes: 6

Related Questions