0

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.

Community
  • 1
  • 1

1 Answers1

0

You have to understand that some people are at school or work, and not all the day on SO answering people's questions, that could furthermore be easily answered by google-ing and studying the documentation.

Anyway, here's a fully commented piece of code:

function getData(callback) { // You pass a callback function as a parameter
    var formData = {
        'player_id' : $('#post-v').attr('data-val')
    };
    $.ajax({
        type: 'post',
        url: '/popins/player/controls',
        data : formData,
        dataType : 'json', // data passed in the success function will going to be parsed if you specify the `dataType`.
        success: function(data) {
          // on success, you call the callback function, passing the data you just got
          callback(data);
        },
        error: function(jqXHR, textStatus, errorThrown) {
          // on error, just log the error.
          console.log(errorThrown);
        }
    });
}

getData(function(data) { // You are calling the `getData` function, passing another function as a parameter, which is going to get called on success

    // This will log the object that you asked your server for.
    console.log(data); 
});

The function getData queries the server for data and then calls the function that you are passing as a parameter, in this case an anonymous function. You could easily just call getData like this, doing basically one more step:

function analyseData(data) { 
    console.log(data); 
}
getData(analyseData);

Now if you have any more questions, please ask them, but be respectful. SO people are not your employees or teachers here to explain you how this and this works. You ought to do some research by yourself. On this note, have a nice evening!

nicovank
  • 3,157
  • 1
  • 21
  • 42