// you can store two configs in two separate files
// FILE NAME: config-development.json:
// FILE CONTENT: {"url":"http://localhost:8000"}
// FILE NAME: config-production.json:
// FILE CONTENT: {"url":"http://google.com"}
// then load correct one and save it to variable
var config = getConfig();
function getConfig() {
// if you pass ?dev=true to your url address default config that will be used is `config-development`
// otherwise - `config-production`
var configName = getParameterByName('dev', false) ? 'config-development' : 'config-production';
window._config || (window._config = {});
// for production version you should concat your config with main script or put it above main script
// inside global `_config` variable for example
if (window._config[configName]) return window._config[configName];
// for development version you can just make an ajax call to get the config
$.ajax({
url : 'app/' + configName + '.json',
async : false,
success : function(response) {
window._config[configName] = response;
}
});
return window._config[configName];
}
// helper
function getParameterByName(name, defaults, location) {
location = location || window.location.href;
name = name.replace(/[\[]/,'\\\[').replace(/[\]]/,'\\\]');
var result = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(location);
return result === null ? defaults : decodeURIComponent(result[1].replace(/\+/g, ' '));
}
// usage
console.log(config.url); // `http://localhost:8000` for development env
// `http://google.` for production env
고맙다 - :
여기내가 그것을 사용하는 방법입니다. 아마 localStorage에서'env' 변수를 수동으로 설정하는 것만으로도 잘 동작 할 것입니다 (dev = true를 매번 URL에 추가하는 것을 기억할 필요가 없을 것입니다). – scttnlsn