Injecting dependencies into an Ember model

I'm attempting to inject dependencies into my Ember models.

https://github.com/emberjs/ember.js/issues/3670, states that this is disabled. Following a link to https://github.com/stefanpenner/ember-cli/blob/master/blueprint/app/app.js#L4 demonstrates how to enable MODEL_FACTORY_INJECTIONS, which in theory, should allow me to inject dependencies into my models, however I'm having no luck.

In the absence of this solution, are there other ways to inject a ref to an app-wide, global singleton object into an Ember.Model besides just dumping it onto the app's namespace (ie. App.ImAGlobalconfig`)?

for reference, this is the initializer I'm working on

App.initializer({
  name: 'preload',
  initialize: function(container/*, application*/) {
    App.deferReadiness();

    Ember.$.ajax({
      url: CONFIG.configURL,
      dataType: 'json',
      context: this
    }).done(
      function(json/*,status, request*/) {
       var appConfig;

        // ...

        container.register('app:config', appConfig, {instantiate: false});
        container.injection('controller', 'appConfig', 'app:config');
        container.injection('route', 'appConfig', 'app:config');
        container.injection('view', 'appConfig', 'app:config');
        container.injection('model', 'appConfig', 'app:config'); // I don't work!

        App.advanceReadiness();
      }
    ).fail(
      function(status, request, error) {
        console.log('Unable to load config with: ' + error);
      }
    );
  }
});

Thanks for any and all help/opinions.

Answers:

Answer

Model injections does indeed work:

Ember.MODEL_FACTORY_INJECTIONS = true;    
App = Ember.Application.create();

App.initializer({
  name: 'model-injection',

  initialize: function(container, application) {

    var cfg = {version: '1'};
    container.register('app:config', cfg, {instantiate: false});


    container.injection('model', 'cfg', 'app:config');
    App.advanceReadiness();
  }
});

App.deferReadiness();

http://emberjs.jsbin.com/lomiy/1/edit

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.