Having trouble with callbacks while calling a service function
This is the function defined in registrationService
function checkUserAccess(incentiveLevel, callback, displayRegistrationView) {
var url = "...";
httpWrapperService.get(url)
.then(
function done(data) {
var hasPermission = incentiveLevel <= data.Level;
callback(hasPermission);
if (displayRegistrationView && hasPermission == false) {
showRegistrationViewForLevel(incentiveLevel);
}
},
function error(errorObject) {
alert("User does not have access\r\nTODO : show popup for registration/login");
}
);
return false;
}
In my directive, I use this function as:
function authenticate() {
registrationService.checkUserAccess(2, function (hasPermission) {
if (hasPermission == false){
return false;
}
else{
return true;
}
});
}
function retrieveDocs() {
var target = authenticate()
if(target)
{
//load both private and public
}
else
{
// load only public
}
}
The issue at hand is with another function retrieveDocuments
, when the user is logged in it should enter into the if(target)
block. However, during debugging, it indicates that target is undefined, resulting in the control flowing to the else part, incorrectly. It seems like there's a callback problem but unsure how to resolve it.
Your assistance would be greatly appreciated. Thanks