Irlanco
Irlanco

Reputation: 779

Need to manipulate a global var, but can't in Ajax. Need a way around this

http://pastebin.com/x5UnA1sE

Here's a paste of a bit of my troubled coded.

I'm trying to manipulate a global variable "data" within the jQuery.get callback function in order to format the data and return this data where it is needed.

However, this global variable is not manipulated at all in the callback function most likely due to Ajax being asynchronous.

How can I get the information I need from the database and eventually return it into a variable as I'm trying to do in this code?

Any direction would be much appreciated!

Thanks

Upvotes: 0

Views: 129

Answers (4)

Ohgodwhy
Ohgodwhy

Reputation: 50787

I'm going to elaborate a bit more on what Shyju said.

Firstly, whether ajax is asynchronous or not isn't the issue. You can always disable it by using asynch: false and having synchronous requests.

What the issue is here is how you're wanting to access and manipulate the data being returned to the server. This is the key piece.

Let's break down where to make the changes

$.get( url, map, receive_map_points){});

In the above callback must be accessed with a function declaration, you cannot replace this declaration with another function you've made for yourself, instead, you can name the object, and pass it to another function to have the data held inside the object manipulated. The key element here is that we need to ensure that we send the object back as a JSON encoded array.

jQuery.get( url, map, function(data){
  //access the data as a json object, extract what we need
  var ddoc = data.doc;
  var dtextrank = data.textrank;

  //pass the newly created data to our function, and have the function extract what it needs.
  receive_map_points(ddoc, dtextrank);
});

function receive_map_points(doc, textrank){
  //same code you used originally.
}

As you can see in the above, we needn't change any code in the receive_map_points function, but we do need to change the data being sent back to the function as a JSON encoded array, example to follow in php:

json_encode(array("doc" => "something", "textrank" => "something else"));

Hope this helps.

Upvotes: 0

dievardump
dievardump

Reputation: 2503

I think you missed the points asynchroneus and callback You know the words, but you don't know how handle these behavior.

asynchroneus means you have to use callbacks -» means that your code must be efficient, you don't put things the one after the others. You have functions, all of these functions do one thing, and call the specified callback when they have finished to do what they are meant to.

What you have to do is not expecting a return from your mapPoints but calling a callback function when you finished manipulate your datas.

Just a little example of using callbacks

function displayPoint(data) {
    //display all the points
}

function callServer(callback {
    ajaxCall(url, function(data) { dataHandler(data, callback); } );
}

function dataHandler(data, callback) {
    // modify data

    callback(data);
}


// process
callServer(displayPoint); 

/***
call callServer by passing displayPoint as callback
callServer will perform the ajax call.
The result will be handle by the anonymus function.
This function just call the dataHandler function, passing it the datas from the server, and the callback function given to callServer ( so displayPoint )
dataHandler will modify the datas, then call the callback function, passing it the datas.

YOU DON'T NEED GLOBAL VAR ANYMORE, and that is a really good thing
***/

Upvotes: 1

thecodeparadox
thecodeparadox

Reputation: 87073

It would be better if you do something like that

function mapPoints() {
    var url = '../common/ajax/get_map_points.php'; 
        map = {}; 

     $.get( url, map, recieve_map_points,function(data){
         getMyData(data);  // call function when you receive data
     });       
}

function getMyData(data) {
  // make something with data
}

Upvotes: 0

Shyju
Shyju

Reputation: 218732

What if you access the data variable inside the callback function ?

function mapPoints()
{
    var url = '../common/ajax/get_map_points.php'; 
    var map = {}; 
     $.get( url, map, recieve_map_points,function(data){
          console.log(data);         
          return data;
     });       
}

Upvotes: 0

Related Questions