I have a single configService
in my AngularJS project that fetches some configuration values of the whole project from the server via an ajax request, i.e. like whether or not user's need to be moderated before their account is activated.
To display information according to the configuration, the whole first page load should be deferred until this ajax request completed. My service looks like:
angular.module('clientApp').factory('configService', function ($http) {
var configService = {};
var conf = {};
Object.defineProperty(configService, 'serverConfig', {
get: function () {
return conf;
}
});
$http.get('/api/config').success(function (data) {
conf = $.extend(conf, data);
});
return configService;
});
So, since the service is a singleton, this will only be executed one time when the page loads, not on every route change.
Now I know how to use $q
and promises, but my problem is how can defer the execution of ALL of angular until this service has finished its request? Most of my views will require values from configService.serverConfig
and depend on it for specific behaviour - doing this asynchronously and have a defered.then()
in every controller does not seem like the best idea.