My Single Page Application (SPA) is in need of some initial data fetched from the server, such as user login status and permissions. However, I want to defer loading the page until this vital information is retrieved to avoid any issues with incomplete data. While using a promise may seem like a solution, it doesn't address my concern about the page starting to load prematurely or the hassle of constantly relying on promises for basic data retrieval.
Here's what I have attempted so far:
Service Code:
app.factory('AppData', function($q, $http){
var appData = {};
$http
.post( "/api/GeneralActions/getInitData", {
}).success(function (data, status, headers, config) {
appData = data;
}).error(function (data, status, headers, config) {
});
return {
getAppData: function () {
return appData;
}
};
});
Page Controller Code:
app.controller('MainPreferences', function($scope, AppData){
// At this point, appData is null
$scope.appData = AppData.getAppData();
});