Custom ENV vars in ember-cli


#1

I added SKYDNS_HOST var below in config/environment.js file.

module.exports = function(environment) {
  var ENV = {
    modulePrefix: 'skydns',
    environment: environment,
    ...
    },

    APP: {
      // Here you can pass flags/options to your application instance
      // when it is created
      SKYDNS_HOST: 'http://172.16.0.11:8080',
    }
  };
  if (environment === 'development') {
  ...
  }

Now how do I access the variable? I tried with window.ENV, window.MyApp.ENV. I even tried putting that variable in side if (environment === 'development') { as I found via google. But nothing seems to work?

So, how do I set and access config variables in ENV ?

I found some solution that says to put in Initializers. But that won’t be ENV specific approach!


#2

MyAppENV.APP.SKYDNS_HOST


#3

Well, I can’t even access the MyAppENV.

I created the app with the name skydns.

ember new skydns.

So does it mean that I can access the constant SkyDnsENV? It says its undefined.


#4

Yes I think it must be SkydnsENV.APP.SKYDNS_HOST or skydnsENV.APP.SKYDNS_HOST It worked like this in Ember-CLI 0.0.44 and I think it still works in 0.1.1 like this but am not sure.


#5

One more thing to notice is:

This app uses ember-cli 0.1.0.

I’d another app previously which uses 0.0.40 and I had scaffolded it as ember new lgebs. In this app, I can access Lgebs constant.

Is there any changes in recent version of ember-cli?


#6

Can you scaffold one app with recent version of ember-cli and test it out?


#7

I installed firebug lite and I see the ENV in Ember-CLI 0.0.44 but not in 0.1.1?? I will try to find where it is moved to.


#8

Yup, that is the case. It might be moved or removed. Yes, do please try to find it.


#9

Found it! It is now direct under your appname skydns.SKYDNS_HOST. You can see it in Firebug.


#10

It did not. says skydns is undefined.

What is the ember-cli and emberjs version that you tried upon?


#11

0.1.1 What do you see in firebug?


#12

Well I’m having a hard time.

Updated the ember-cli to 0.1.1 and scaffolded a new app with name Galo

ember new galo

And I can access it as Galo, not galo.

± ember serve
version: 0.1.1
Livereload server on port 35729
Serving on http://0.0.0.0:4200

Build successful - 265ms.

Slowest Trees                  | Total
-------------------------------+----------------
Concat                         | 68ms
ES6Concatenator                | 24ms
ES3SafeFilter                  | 23ms
JSHint - App                   | 21ms
JSHint - Tests                 | 15ms
CustomStaticCompiler           | 15ms

And now this is with my existing app. I updated ember-cli with npm link ember-cli and ran it. But I don’t get the app constant. Neither with SkyDns nor skydns.

± ember serve
version: 0.1.1-master-7e3d3bd055
Livereload server on port 35729
Serving on http://0.0.0.0:4200/

Build successful - 1611ms.

Slowest Trees                  | Total
-------------------------------+----------------
TreeMerger (appAndDependencies) | 218ms
TreeMerger (stylesAndVendor)   | 176ms
ES6Concatenator                | 151ms
TreeMerger (ExternalTree)      | 138ms
TreeMerger (vendor)            | 101ms
JSHint - App                   | 94ms
CustomStaticCompiler           | 92ms


#13

Did you check if the index.html file in your app folder has the same structure for both projects? My first guess is they are not.


#14

I don’t believe this has been said yet, but…I believe you can just import the ENV variables like so:

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


#15

Well I’ve tried this before putting the question here and at that time, this approach was not working either.


#16

Ok, let me check and verify it.


#17

Well, I finally got it nailed. The main change was the "ember-export-application-global": "^1.0.0", module. I think this was extracted out in separate module when coming near to 0.1.1 of ember-cli. @broerse Thanks for your input! I might post a blog about this.


#18

Add a link when you do.


#19

hi all! @millisami do you find the solution ? Can you please share with us ? I have the same problem and I don’t know how to access the variables from the environment file.

Thanks


#20

Example: