Search code examples
ember.jsember-cliember-addon

How to read Ember-cli config file from addon?


Is there another way besides reading config file with

this.container.lookupFactory('config:environment').modulePrefix;

inside and object initializer? Seems a bit odd to do this


Solution

  • With ember 2.4.x (the LTS version which I recommend using), you can do the following:

    import Ember from 'ember';
    import layout from './template';
    
    export default Ember.Component.extend({
      layout,
    
      magicKey: Ember.computed.reads('config.magic.key'),
    });
    

    This assumes that you have set the config/environment.js file as follows:

    module.exports = function(environment) {
      var ENV = {
        /* .... */
    
        APP: {
          // Here you can pass flags/options to your application instance
          // when it is created
        },
    
        magic: {
          key: "something awesome"
        },
      };
    
      return ENV;
    };
    

    EDIT: Adding how to get the config object below, to answer the question from @jcbvm.

    For this to work, you will need to export the config in your app directory in your addon as follows: (in your addon_name/app/component/component_name/component.js )

    import config from '../../config/environment';
    
    export default component.extend({ config });