Just making sure I understand correctly, I have a simple web service resource query function set up like this
//Get country lists
$scope.getCountry = function(){
$http({
method : 'GET',
url : cdnLinks('country'),
}).success(function(data) {
$scope.countries = data;
}).error(function(data) {
console.log(data);
});
};
//Get profile
$scope.getProfile = function(profile_id, select){
var formatJSON = {
profile_id: profile_id,
select: select,
};
var json = JSON.stringify(formatJSON);
var urlsafe = exEncrypt(json);
ProfileService.profileActions.query({payload:urlsafe}).$promise.then(function(datas){
$scope.uProfile = datas[0];
document.querySelector('title').innerHTML = $scope.uProfile.username;
});
};
//Initialize update sequence
$scope.initUpdate = function(profile_id, select){
$scope.getCountry();
$scope.getProfile(profile_id, select);
}
Essentially, when a user clicks the "Update" button, the
ng-click="initUpdate(param1, param2)"
function is triggered to load and display all necessary information. This function works well, but there is a problem. Due to getCountry()
typically returning a larger file size [~3 - 3.5kb, load time ~260++ms server dependent] compared to getProfile()
[~1 - 2kb, load time ~200++ms server dependent], the profile may be displayed before the country list is fully loaded, resulting in some empty fields on the UI page.
To address this issue, initially I tried applying a timeout to getProfile()
like this
$scope.initUpdate = function(profile_id, select){
$scope.getCountry();
$timeout(function(){
$scope.getProfile(profile_id, select);
}, 200);
}
While this workaround worked temporarily, I found it challenging to determine the exact amount of delay needed as it depends on the server's loading time.
I am interested in exploring alternative approaches to ensure that
- All necessary resources are fully loaded and COMPLETE (
getCountry()
,getInterest()
,getABCDEFG()
, and other similar functions...) - Only the
getProfile()
function is executed?