12
votes

I'm trying to inject an Ember service into an Ember Object but keep getting the following error:

"Assertion Failed: Attempting to lookup an injected property on an
object without a container, ensure that the object was instantiated 
via a container."

My code looks essentially something like the following:

const Model = Ember.Object.extend({
  store: Ember.inject.service(),

  destroyRecord() {...},

  serialize() {...},

  deserialize() {...},
});

let newModel = Model.create();
newModel.get('store');

Note: it does work if I inject the service into a Controller, but not an object. Haven't had any luck trying to figure out how to register the Object with the Ember container.

2

2 Answers

19
votes

It works for an Ember.Controller because Ember controls the lifecycle of the object. In short, when Ember needs an instance of a certain controller, it asks the container for one, and the container will provide the instance, initializing if necessary.

What this implies is that for dependency injection to work, you would need to get a new instance of Model through the container. Assuming Ember 2.3 because of getOwner, and that this is somewhere inside the Ember application:

let owner = Ember.getOwner(this);
let newModel = owner.lookup('object:model');
newmodel.get('store');

You can consult the lookup documentation here.

But how to register? Use an application initializer:

$ ember generate initializer register-model

Then, open up the generated initializer and, assuming that your file is in app/folder/model.js, put something like:

import Model from 'app-name/folder/model';

export function initialize(application) {
  application.register('object:model', Model);
}

export default {
  name: 'register-model',
  initialize
};

You can consult the register documentation here.

Hope this is helpful!

0
votes

Well you need to passing in the container instance when you create a instance of your model. The container is accessible in the route, controllers, components with this.get('controller'). AFAIK basically anything created with the container gets the container property set. Thats why service injections work in controllers etc..

So if you are creating the model in a route's method. The code will look like below

App.IndexRoute = Ember.Route.extend({
  model: function() {
    var newModel = Model.create({
      container: this.get('container')
    });    
    return newModel.get('test').getText();
  }
});

Here is a working demo.