Skip to content

Instantly share code, notes, and snippets.

@Bolza
Created June 9, 2015 13:14
Show Gist options
  • Save Bolza/ddf9872fbb62f401392d to your computer and use it in GitHub Desktop.
Save Bolza/ddf9872fbb62f401392d to your computer and use it in GitHub Desktop.
Attempt to make config loading syncronous in AngularJS
(function() {
'use strict';
var envconfig = angular.module('app.envConfig', []);
envconfig.config(confFunc);
envconfig.provider('startup', startupFactory);
confFunc.$inject = ['startupProvider'];
/* @ngInject */
function confFunc(startupProvider) {
}
startupFactory.$inject = [];
/* @ngInject */
function startupFactory() {
var obj = {
url: 'config/dev.config.js'
};
var usingHTTP = ['$http', function($http) {
$http.get(obj.url).success(function(data) {
obj = data;
});
return obj;
}];
function usingXHR(cb) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function (resp) {
if (xhr.readyState !== 4) {
return;
}
obj = angular.fromJson(xhr.responseText);
};
xhr.open('GET', obj.url, false);
xhr.send();
return obj;
}
function usingAJAX() {
var xhr = jQuery.ajax({
type: 'GET', url: obj.url, cache: false, async: false, contentType: 'application/json', dataType: 'json'
});
if (xhr.status === 200) {
angular.extend(obj, angular.fromJson(xhr.responseText));
}
return obj;
}
/*jshint validthis: true */
this.$get = usingAJAX;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment