如何使用后回调的值 [英] how to use the value after the callback

查看:160
本文介绍了如何使用后回调的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我隔离了一个方法中的网络,并执行回调以获取JSON响应,我以后如何使用它?

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 getJson(url, callback) {
    Ti.API.info(" im here " + url);
    var jsonObject;
    var xhr = Ti.Network.createHTTPClient();
    xhr.setTimeout(3000);
    xhr.onload = function () {
        var jsonObject = eval('(' + this.responseText + ')');
        callback.call(jsonObject)
    }
    xhr.open("GET", url);
    xhr.send();
    Ti.API.info(" passed ");
};

function acallback() {
    return this;
}



现在我的问题是,我如何使用返回的输出?

Now my questions is , how can i use the returned output afterward ?

推荐答案

应如下所示:

function getJson(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 " );
}

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

getJson(url , acallback);

请注意,我删除了使用 eval 此处):

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


eval函数非常快。但是,它可以编译和执行
任何JavaScript程序,因此可能存在安全问题。当源是可信和有能力时,指示使用
eval。使用JSON解析器更安全多了
。在通过XMLHttpRequest的Web应用程序中,
通信仅允许提供该
页面的同一来源,因此它是受信任的。但它可能没有能力。如果服务器
在其JSON编码中不严格,或者如果不严格
验证其所有输入,那么它可以传递无效的JSON文本
可能携带危险的脚本。 eval函数将
执行脚本,释放其恶意。

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.

此外,最好使用 var 关键字只对范围一次。

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

如果要在回调范围之外使用生成的json对象,只需在需要json的范围内定义回调,例如:

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);
    }
}

或者,如果您想使用调用选项,您可以这样做:

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

function getJson(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
}

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

getJson(url, myCallback, obj);






第三个编辑



如果我们讨论这个问题,我建议使用一个很好的技巧,它在 Prototype MooTools jQuery ,根据MDN,在JavaScript 1.8.5 中引入。


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;

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

您可以阅读教程,其中我从中复制到代码。

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

这篇关于如何使用后回调的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆