Skip to content

Instantly share code, notes, and snippets.

@jsmecham
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsmecham/8948a8519559bbba4d15 to your computer and use it in GitHub Desktop.
Save jsmecham/8948a8519559bbba4d15 to your computer and use it in GitHub Desktop.
Ember CLI Environment Injection Example
//
// Illustrates a potential method for injecting environment settings into the
// Ember environment provided by Ember CLI as a serialized JSON blob in a
// META tag.
//
// You could return this from a backend, such as Rails, that is aware of the
// variables for the given environment and load this with a SCRIPT tag before
// loading the Ember application.
//
(function() {
var MyDynamicAppENV = {
stripeKey: '<%= ENV['STRIPE_PUBLISHABLE_API_KEY'] %>',
segmentIoKey: '<%= ENV['SEGMENTIO_SECRET'] %>'
}
function updateEnvironment() {
var environmentMetaTag = document.querySelector('meta[name="dashboard/config/environment"]'),
environment = decodeEnvironment(environmentMetaTag.content);
if (typeof environment['APP'] == 'undefined') {
environment['APP'] = {};
}
Object.keys(OrchestrateENV).forEach(function(env) {
environment['APP'][env] = MyDynamicAppENV[env];
});
environmentMetaTag.content = encodeEnvironment(environment);
}
function decodeEnvironment(environment) {
var decodedEnvironment = decodeURIComponent(environment),
parsedEnvironment = JSON.parse(decodedEnvironment);
return parsedEnvironment;
}
function encodeEnvironment(environment) {
var stringifiedEnvironment = JSON.stringify(environment),
encodedEnvironment = encodeURIComponent(stringifiedEnvironment);
return encodedEnvironment;
}
updateEnvironment();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment