2

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

abFx
  • 313
  • 3
  • 14

1 Answers1

1

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 });
physphun
  • 376
  • 3
  • 6
  • Where is the `config` property coming from in your component? – jcbvm May 08 '16 at 11:10
  • @jcbvm I added instructions on how to get the `config` in the main answer. – physphun May 09 '16 at 14:30
  • I see, this should work within an app indeed, but not within an addon, you could therefor also use https://github.com/null-null-null/ember-get-config – jcbvm May 09 '16 at 18:02
  • I think you misunderstood me. This works within an addon. In the root of the addon, there is an `app` folder in which you can import and export the config as I have shown. Let me know if this is still confusing. I will add a more explicit reference in the answer. – physphun May 09 '16 at 20:28
  • Somehow the answer was not shoing the full path. I have added that in! – physphun May 09 '16 at 20:30