1

I'm creating an Ember CLI ember-addon, and in my addon's files I need access to the app's config. I have no way of knowing what the app including this addon will be named, so I can't simply do import ENV from 'app-name/config/environment' like I might in the application itself.

How can I access the namespace of the application that is using the ember-addon from within the addon itself, so that I can import things from that application?

Kerrick
  • 7,420
  • 8
  • 40
  • 45

2 Answers2

2

You should not need to get the namespace in order to get the config.
Any setting that your addon requires should be added on to ENV.APP in config/environment.js.

For example if you wanted a MY_APP_KEY setting you would require that something like
ENV.APP.MY_APP_KEY = 'ABCDEF'; was added to config/environment.js.

You can then use an initializer to read the property off of the application instance and inject it into you addon by doing something like...

export default {
  name: "my initilizer",

  initialize: function(container, app) {

    //get you setting off of the app instance
    var key = app.get('MY_APP_KEY');

    //register it
    app.register('config:myAddonKey', key, { instantiate: false });

    //inject it where you want to access it
    app.inject('route', 'myAddonKey', 'config:myAddonKey'); 
  }
};

You can see an example of how its done in the Ember Django Adapter

tikotzky
  • 2,542
  • 1
  • 17
  • 20
  • I won't reliably have access to the app or the container. For example, what about a simple `utils/asset-host.js` that cares about `ENV.environment`? – Kerrick Sep 02 '14 at 22:29
0

One possibility is to use an initializer:

Ember.Application.initializer({
    name: 'my-component',
    initialize: function(container, app) {
        // you have access to 'app' here...
    }
});
Steve H.
  • 6,912
  • 2
  • 30
  • 49