I posted a similar question, but since stack overflows go dead real quick, I feel like I didn't get an answer.
Sometimes I don't understand things unless it's done to my code. People tend to explain things really awfully. Example: How do I return the response from an asynchronous call?
In that first answer, he explains how to restructure the code. However he doesn't explain the steps correctly. He doesn't explain what callback is or what result is. He just throws them in there. I'm assuming he's giving multiple examples on how to do it, but he isn't explaining what to use.
The following is my code that I had originally:
var admin_data = false;
function getPlayerAdmin(admin_data){
var formData = {
'player_id' : $('#post-v').attr('data-val')
};
$.ajax({
type: 'post',
url: '/popins/player/controls',
data : formData,
dataType : 'json',
success: function(admin_data_vals, admin_data) {
if(admin_data_vals.controls.is_admin == true){
admin_data = true;
}else{
admin_data = false;
}
},
error : function(jqXHR, textStatus, errorThrown){
console.log(jqXHR);
}
});
}
getPlayerAdmin();
alert(admin_data);
The following is what I tried to restructure based on his... example:
var admin_data = false;
var result = getPlayerAdmin();
getPlayerAdmin(function(result) {
if(admin_data_vals.controls.is_admin == true){
admin_data = true;
}else{
admin_data = false;
}
});
function getPlayerAdmin(callback){
var formData = {
'player_id' : $('#post-v').attr('data-val')
};
$.ajax({
type: 'post',
url: '/popins/player/controls',
data : formData,
dataType : 'json',
success: function(admin_data_vals, callback) {
},
error : function(jqXHR, textStatus, errorThrown){
console.log(jqXHR);
}
});
}
getPlayerAdmin();
alert(admin_data);
I'm trying to get the variable out of here so that I can use it in another function.
I'm so confused, help is greatly appreciated.