I am using a callback method to wait for an ajax request to complete that checks if a different person has locked an item.
The callback method simply waits for the ajax request to complete, then pushes the resultant message into the html.
Here is the ajax function:
function checkLockAndPushModalMessage(url, evaluationId, userIsObserver, callback) {
console.log('we are in checkandpush');
url = url + evaluationId + "/"
$.ajax({
url: url,
type: 'GET',
beforeSend: function (xhr, settings) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
},
success: function (data) {
if (userIsObserver){
console.log('user is observer (in success in check & push)')
if (data.employee_locked === true) {
callback(true);
} else {
callback(false);
}
}
if (!userIsObserver) {
console.log('user is non-observer (in success in check & push)')
if (data.admin_locked === true) {
callback(true);
} else {
callback(false);
}
}
},
error: function(jqxhr, textStatus, errorThrown) {
// TODO: Handle errors
console.log('There was an issue determining if the other use has locked the evaluation!');
},
})
};
The callback method is this:
function messageToModal(userHasLocked) {
let isLocked = userHasLocked;
console.log('in messageToModal');
console.log('other user has locked is: ' + userHasLocked);
console.log('isLocked is: ' + isLocked);
console.log(typeof userHasLocked);
if ('foo' != 'bar') {
// This evaluates fine and logs in console
console.log('Foo is NOT Bar!')
}
let modal_message = "NO NO NO";
if (isLocked === true) {
// The other user has locked the evaluation
let modal_message = "The other user has locked this evaluation.";
}
if (isLocked != true) {
// The other user has not locked the evaluation
let modal_message = "The other user has not locked this evaluation.";
}
// Push the message letting them the know the other users locked status to the modal
console.log('gotta push the message now: '+ modal_message);
$("#lockControlMessage").text( modal_message );
}
And I call it like this:
checkLockAndPushModalMessage(evaluationUrl, evaluationData.id, userIsObserver, messageToModal);
There is a lot of 'dummy data' in there just so I can see what is happening and where we're at in the console.
The other if statement I have in there works as you would expect.
The console looks like this: