17

I added some configurations to myapp/config/environment:

if (environment === 'development') {
  ENV.APP.AuthURL = 'http://localhost:5000/';
}

Now, to access this configuration should I use some method or directly accessing window.Myapp?

Mateus Vahl
  • 979
  • 2
  • 12
  • 29

3 Answers3

29

You can access it by importing environment.js using the line below:

import config from '../config/environment';

For example, lets say you want to access your configuration in a controller. This is what it would look like:

import Ember from 'ember';
import config from '../config/environment';

export default Ember.Controller.extend({
  foo: config.APP.AuthURL
});

If you need to, you can now access it in your controller's template using:

{{foo}}
rog
  • 5,351
  • 5
  • 33
  • 40
7

There are a couple modern ways, as of this writing, when trying to access it from your application:

  1. import ENV from 'your-application-name/config/environment';
    • your-application-name should be what's in the modulePrefix key of config/environment.js and the name key of package.json
  2. Via Ember.getOwner(this).resolveRegistration('config:environment');

Number one assumes you're using Ember CLI and is detailed in the ember docs under Configuring Your App:

Ember CLI ships with support for managing your application's environment. Ember CLI will setup a default environment config file at config/environment. Here, you can define an ENV object for each environment, which are currently limited to three: development, test, and production.

The ENV object contains three important keys:

  • EmberENV can be used to define Ember feature flags (see the Feature Flags guide).
  • APP can be used to pass flags/options to your application instance.
  • environment contains the name of the current environment (development,production or test).

You can access these environment variables in your application code by importing from your-application-name/config/environment.

jabbascript
  • 345
  • 1
  • 6
  • 13
1

While @rog's answer is correct and will work for all cases where you are trying to access the config from your application there are some edge cases (such as accessing config from an addon) that it will not work for.

I would recommend checking out the ember-get-config addon: https://www.emberobserver.com/addons/ember-get-config

Once you install ember-get-config you can import your config using the following code:

import config from 'ember-get-config';
const { AuthURL } = config;
// now you have access to AuthURL 

This will work in your application and it will also work if you build an addon that will be consumed by your application

real_ate
  • 10,861
  • 3
  • 27
  • 48
  • So you need an add-on to get the pre-configured environment.js? Very strange, but looking into it. More so is why Ember doesn't ship with this. – Andrew Lank Sep 20 '19 at 14:56
  • So that's not quite true, you can get the config directly by just importing it. I tend to use ember-get-config because it deals with some issues when writing Ember addon code and you want to get the config of the app that has installed the addon. Essentially this is the "safer" way to get config. Also it adds no runtime overhead so it's not like you're adding to your bundle size when adding this addon – real_ate Sep 23 '19 at 08:38