Last active
August 29, 2015 14:09
-
-
Save jsmecham/8948a8519559bbba4d15 to your computer and use it in GitHub Desktop.
Ember CLI Environment Injection Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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