How to access environment variables in .profile for a controller? - ember.js

I have a problem accessing the environment variables in my EmberJs app.
I want to use the environment variable set in the .profile file of the server.
My .profile in the server
export SERVER_NODE=testNode
I already tried this solution :
process.env like node.js in my config/environment.js but it doesn't
work, i cannot access it in my app (Stack Post here)
So if you have any idea about how I can do this, I take it.
Thanks you.

Try using the ember-cli-dotenv Ember addon: https://github.com/fivetanley/ember-cli-dotenv
This will let you access variables from a .env file in config/environment.js. You can then access these variables from a component or service by importing config/environment.js.
Example:
.env
CLIENT_KEY=THEKEY
CLIENT_SECRET=SECRETSECRET
config/environment.js
module.exports = function(environment) {
var ENV = {
API: {
clientKey: process.env.CLIENT_KEY,
clientSecret: process.env.CLIENT_SECRET
}
}
};
app/services/example.js
import Ember from 'ember';
import ENV from '../config/environment';
export default Ember.Service.extend({
clientKey: ENV.API.clientKey, // 'THEKEY'
clientSecret: ENV.API.clientSecret // 'SECRETSECRET'
});

Related

EmberJS expose env variable as route

I would like to configure a URL differently in my ember app depending if the app is running in production or development mode. Is this possible to do by loading the endpoints into config/environment.js and accessing the value of the ENV object in a handlebars template? Maybe I have to expose the ENV value in a controller? Or is there some other mechanism for doing this?
Something like this:
// config/environment.js
if (environment === 'development') {
ENV.zzz.url = "http://www.reddit.com";
}
if (environment === 'production') {
ENV.zzz.url = "http://www.google.com";
}
// app/controllers/application.js
export default Ember.Controller.extend({
siteUrl: ENV.zzz.url
...
// somefile.hbs
Funspot
yes. you can do import config from 'app-name/config/environment';
and access it like config.zzz.url

Acces environment.js from Ember addon index.js

I am building a Ember addon and I will like to know if there is a way to access the config/environment.js file from the root index.js. Basically I would like have the same config value used from the root index.js and a component.
Thanks
const config = this.project.config()
You can simply require it:
// index.js
var environment = 'production'; // optional
var ENV = require('config/environment')(environment);
console.log(ENV.baseURL);

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
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 });

Best way of passing arguments to ember addon source code

TL;DR
Is it possible to pass the options from the addon's index.js file to the addon's Ember application?
More detail
Ember CLI addon's allow you to pass options to an addon by setting options in your Brocfile.js.
var EmberApp = require('ember-cli/lib/broccoli/ember-app');
var app = new EmberApp({
myAddon: {
enabled: false // TODO - Change when CLoudfront is implemented
},
});
You can then access these options in the addon's index.js file like so:
module.exports = {
name: 'my-addon',
included: function(app) {
var options = app.options.myAddon;
}
}
However, what if I want to use these options in an Ember component or view or other class in the addon? For example, a component:
import Em from 'ember';
export default Em.Component.extend({
doSomething: function() {
if (this.get('options.enabled')) {
// Then do something
}
},
});
Is it possible to pass the options from the addon's index.js file to the addon's Ember application?
Please note, I'm aware you could just pass the argument to the component in the template but that's not the point of this question. Thanks.
I think what you're trying to accomplish is best implemented using config/environment.
You can either add your options to your addons config/environment.js file. This object will be merged with you applications config/environment.js.
module.exports = function(/* environment, appConfig */) {
return {
myaddon {
enabled: true
}
};
};
Or if you are feeling funky, you can override the default behaviour of the addons config hook in index.js to manually add the options. But the happy path should be considered adding it to config/environment.js
In your component you can then import the config with:
import config from '../config/environment';
If you also need the options to be added to your app instance. You can add the options under the APP property in config/environment.js. The content of the APP property are added to the app instance. In your addon's config/environment.js put:
module.exports = function(/* environment, appConfig */) {
return {
APP: {
// Here you can pass flags/options to your application instance
// when it is created
myaddon {
enabled: true
}
}
};
};

BaseURL in Ember CLI application with IE9 support

For my Ember CLI application I want to use a baseURL, as described here. It works very well for the History API, but for the old Hash API it won't work as expected.
My configuration:
module.exports = function(environment) {
var ENV = {
environment: environment,
baseURL: '/base/',
locationType: 'auto',
...
};
...
return ENV;
}
In IE9 i got localhost:4200/#/base/login instead of localhost:4200/base#/login. Going manually to this page results in a blank (white) page.
I found you have to set the router rootUrl as well as the environment baseUrl.
//router.js
import Ember from 'ember';
import config from './config/environment';
let Router = Ember.Router.extend({
location: config.locationType,
rootURL: config.baseURL
});
export default Router;
It's a known issue apparently: http://github.com/stefanpenner/ember-cli/issues/417