简体   繁体   中英

How can I get the namespace of the including application in an Ember CLI Addon?

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?

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

One possibility is to use an initializer :

Ember.Application.initializer({
    name: 'my-component',
    initialize: function(container, app) {
        // you have access to 'app' here...
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM