JDillon522
JDillon522

Reputation: 19686

Ember js 1.12.0 - Failed to create instance

I'm trying to inject my config file into all of my routes, controllers, and components instead of calling import config from '../config/environment' in every file. I get the following error however:

Uncaught Error: Failed to create an instance of 'config:main'. Most likely an improperly defined class or an invalid module export.

Below is my code as its rendered via coffeescript.

// app/initializers/config.js

define('app/initializers/config', ['exports', 'app/config/environment'], function (exports, Config) {

'use strict';

var ConfigInitializer, initialize;

initialize = function (container, app) {
  var config;
  config = {
    config: Config['default']
  };
  app.register('config:main', config);
  app.inject('route', 'config', 'config:main');
  app.inject('controller', 'config', 'config:main');
  app.inject('component', 'config', 'config:main');
};

ConfigInitializer = {
  name: 'config',
  initialize: initialize
};

exports['default'] = ConfigInitializer;

exports.initialize = initialize;

What am I missing?

I've stepped through everything using breakpoints and my path to my environment.js file is correct. So I know its not that. I think I'm missing something fundamental about dependency injection.

Upvotes: 1

Views: 1598

Answers (2)

Artur Smirnov
Artur Smirnov

Reputation: 804

Everything looks fine in your code, except one thing. By default Ember expects to register Factories but not instances. So once the property gets injected, it tries to get an instance from registered factory. But in your case it's not a factory, it's an instance (object) itself. So the only thing you have to do is to say Ember to use registered object as is, without trying to get an instance. To achive this, just add instantiate: false to register options:

app.register('config:main', config, {instantiate: false});

Upvotes: 3

cefigueiredo
cefigueiredo

Reputation: 738

It is complaining that can't instantiante a new config object when initializing. Try changing your config object into a Ember.Service

import Ember from 'ember'

initialize = function(container, app) { 
  var config = Ember.Service.extend({
    config: Config['default']
  }
  ...
}

Upvotes: 1

Related Questions