1

how do i make data overwrite results variable ?

var ajax = {
    get : {
        venues : function(search){
            var results = "@";
            $.getJSON("http://x.com/some.php?term="+search+"&callback=?",function(data){ results = data; });
            return results;
        }
    }
};
1

4 Answers 4

6

data is overwriting results, just after results has been returned.

You can use the ajax function instead of getJSON, since getJSON is just shorthand for

$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: callback
});

and then also set async to false, so that the call will block.

However, in your case this won't work, because JSONP requests (with "?callback=?") cannot be synchronous.

The other (better) option is to have whatever code is dependent on the results return value get called by the success callback.

So, instead of something like this:

var results = ajax.get.venues('search');
$('#results').html(translateResults(results));

Maybe something like this:

ajax.get.venues('search', function (results) {
    $('#results').html(translateResults(results));
});

venues = function (search, callback) {
    $.getJSON("http://x.com/some.php?term="+search+"&callback=?",
              function(data){
                  callback(data); 
              });
};
Sign up to request clarification or add additional context in comments.

Comments

5

Your problem is the asynchronous nature of JavaScript. results does get overwritten, but only later, after the function has already exited, because the callback is executed when the request has finished.

You would have to make the Ajax call synchronous using sync: true (this is usually not a good idea, just mentioning it for completeness's sake) or restructure your code flow so it doesn't depend on the return value any more, but everything you need to do gets done in the callback function.

Comments

3

This isn't a scope problem. It's because $.getJSON is asynchronous; results is returned before $.getJSON finishes. Try making a callback for $.getJSON to call when it's done.

function JSON_handler(data){
    // do stuff...
}

$.getJSON("http://x.com/some.php?term="+search+"&callback=?", JSON_handler);

Comments

3

You could put the logic you want to run in a callback.

    var ajax = {
      get : {
        venues : function(search, fnCallback){
            var results = "@";
            $.getJSON("http://x.com/some.php?term="+search+"&callback=?",
                function(data){ 
                    // success
                    results = data; 
                    (typeof fnCallback == 'function') && fnCallback(data);
                });
            return results;
        }
    }
};

ajax.get.venues(term, function(result){
    // Do stuff with data here :)
})

functional programming can be fun.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.