Skip to content Skip to sidebar Skip to footer

How To Use The Value After The Callback

In my application i'm isolating the networking in a method and performing a callback to get the JSON response , how can i use it afterward ? getJson(url, acallback); function get

Solution 1:

Should look like this:

functiongetJson(url, callback) {
    Ti.API.info(" im here " + url );
    var jsonObject, xhr = Ti.Network.createHTTPClient();

    xhr.setTimeout(3000);
    xhr.onload = function() {
        var jsonObject = JSON.parse(this.responseText);
        callback(jsonObject)
    }
    xhr.open("GET" , url); 
    xhr.send(); 
    Ti.API.info(" passed " );
}

functionacallback(json) {
      Ti.API.info("data from ajax: " + json);
}

getJson(url , acallback);

Notice that I removed the use of eval since it's bad practice to use that, as it says (here):

The eval function is very fast. However, it can compile and execute any JavaScript program, so there can be security issues. The use of eval is indicated when the source is trusted and competent. It is much safer to use a JSON parser. In web applications over XMLHttpRequest, communication is permitted only to the same origin that provide that page, so it is trusted. But it might not be competent. If the server is not rigorous in its JSON encoding, or if it does not scrupulously validate all of its inputs, then it could deliver invalid JSON text that could be carrying dangerous script. The eval function would execute the script, unleashing its malice.

Also, you better use the var keyword only once per scope.


Edit

If you want to use the resulting json object from outside the scope of the callback, just define the callback in the scope where the json is needed, for example:

var obj = {
    x: 4,
    doit: function() {
        var _this = this;
        var callback = function(json) {
            alert(_this.x * json.y);
        };
        getJson(url , callback);
    }
}

The json.y part I just made up for the example of course.


2nd Edit

Alternatively, if you want to use the call option, you can do this:

functiongetJson(url, callback, bindto) {
    Ti.API.info(" im here " + url );
    var jsonObject, xhr = Ti.Network.createHTTPClient();

    xhr.setTimeout(3000);
    xhr.onload = function() {
        var jsonObject = JSON.parse(this.responseText);
        callback.call(bindto, jsonObject)
    }
    xhr.open("GET" , url); 
    xhr.send(); 
    Ti.API.info(" passed " );
}

var obj = {
      x: 5
}

functionmyCallback(json) {
      alert(this.x * json.y);
}

getJson(url, myCallback, obj);

3rd Edit

If we're on the subject, I recommend using a nice trick, which is used in Prototype, MooTools, jQuery and according to the MDN was Introduced in JavaScript 1.8.5.

Function.prototype.bind = function(scope) {
  var _function = this;

  returnfunction() {
    return _function.apply(scope, arguments);
  }
}

You can read the tutorial where I copied to code from.

Solution 2:

I believe callback.call(jsonObject) should actually read callback(jsonObject). Once you have invoked an asynchronous function, the way to get its value is via a callback. So, the function you pass in as callback will get the result. Say,

functionmyCallback(value) {
    console.log(value);
}

Post a Comment for "How To Use The Value After The Callback"