1

我有一段 js 代码,我真的很想改进,但不知道如何改进。下面的工作版本有一个全局变量和一个单独的函数声明,我认为可以合并到一个匿名函数中(代码如下:不工作)

任何帮助或指示将不胜感激。

工作版本

var Data = {}; // would like to remove the global variable

function callBack() {
    $.ajax({
        url: "http://host/callBacks/script.js",
        //get and execute Script to process json data below
        dataType: "script"
    });
}


$(document).ready(function() {
    $.ajax({
        type: "Get",
        url: "http://host/data/json/",
        success: function(json) {
            Data = json; // Would like to just call the callback here
            callBack(Data);
        },
        error: function() {
            alert("error");
        }
    });
});


// Script which gets loaded from callBack
(function(Data) {
    var json = $.parseJSON(Data);
    $.each(json, function(i, v) {
        alert(v);
    });
})(Data);

所需代码:不工作

// Error: Length is null or not an object. In jQuery script

var Data = {}; // Ideally would like to remove this from global scope if possible

$(document).ready(function() {
    $.ajax({
        type: "Get",
        url: "http://host//data/json/",
        success: function(Data) {
            $.ajax({
                url: "http://host/callBacks/script.js",
                dataType: "script"
            });
        },
        error: function() {
            alert("error");
        }
    });
// Error: Length is null or not an object. In jQuery script

更新:根据 adeneo 的回答:仍然需要全局 Data = {}; 因为返回的立即执行脚本作为参数我想

var Data = {};

$(document).ready(function() {
    function doAjax() {
        return $.ajax({
            type: "GET",
            url: "http://host/data/json/"
        });
    }

    var XHR = doAjax();
    XHR.done(function(data) {
        Data = data; // <--- If I remove this I get Error:'length' is not or not an object
        $.ajax({
            url: "http://host/callBacks/script.js",
            dataType: "script"
        });
    }).fail(function() {
        alert("error");
    });
});

不过似乎还不错。可能应该提到我正在 IE 8 中对此进行测试。约束。更新的标签

4

3 回答 3

3
$(function() {
    function doAjax() { //put inside function
        return $.ajax({ //return deffered object
            type: "GET",
            url: "/data/json/" //X domain not supported
        });
    }

    var XHR = doAjax();  //do ajax call
    XHR.done(function(data) { // use deffered object
        var json = $.parseJSON(data); //why would you need to get an external script just to parse some JSON?
        $.each(json, function(i, v) {
            alert(v);
        });
    }).fail(function() {
        alert("error");
    });
});
于 2012-08-20T05:04:07.330 回答
0

怎么样:

(function () {

    var myData_andMethod = {
        data : {},
        method : function (data) { this.data = JSON.parse(data); }
    };

    $.ajax({success : (function (obj) {
            var callback = obj.method.bind(obj);
            return function (data) {
                callback(data);
            };
        }(myData_andMethod)) //, error : function... url:"".....
    });
    //attach events here.
}());

你现在有 0 个全局的东西。
除了现在你必须确保你对数据做一些事情,一旦你的回调触发(或者它会永远消失,你永远不能再次访问它),或者你需要将它发送到一些在已知范围内的函数(全局或您的应用程序的一部分或其他...),或将其粘贴在可公开访问的 var 中以便您可以在关闭封闭后访问数据,以及所有回调回来。

甚至像将对象发送到将myData_andMethod对象存储在其他位置的其他函数一样简单......

于 2012-08-20T05:31:14.347 回答
0

您可以将全局变量和相关函数管理到一个类中,并从中创建一个对象

$(document).ready(function(){
    // Your code here !
});
于 2012-08-20T05:04:46.053 回答