1

我有两个 javascript 类(Controller.js 和 Events.js)。我从 Events.js 调用 Controller.js 中的 XML 解析器。Parser 工作但不返回任何内容:

SceneEvent.prototype.handleKeyDown = function (keyCode) {
    switch (keyCode) {
        case sf.key.ENTER:
            var itemList = null;    
            itemList = Controller.ParseXML("app/data/Event.xml");   
            alert("itemList = " + itemList);
    }
};

Controller.js 看起来像这样:

Controller.ParseXML = function (url) {
    var itemList = null;

    $.ajax({
        type: "GET",
        url: url,
        dataType: "xml",
        async: false,
        success: function(xml) {
            $(xml).find("event").each(function() {
                var _id = $(this).attr("id");
                var _eventItemDay = $(this).find("eventItemDay").text();
                ...
                var _eventItemLocation = $(this).find("eventItemLocation").text();

                itemList = {
                    id: _id,
                    eventItemDay: _eventItemDay,
                    eventItemLocation: _eventItemLocation,
                    ...
                    eventItemLocation: _eventItemLocation
                };
            });
            return itemList;
        },
        error: function(xhr, ajaxOptions, thrownError){
            alert("XML ERROR");
            alert(xhr.status);
            alert(thrownError);
        }
    });
};

当我在 Controller.js 中打印出 itemList 时,一切正常。有什么建议么?

4

2 回答 2

2

您必须在函数末尾返回值ParseXML,而不是在success函数末尾。

Controller.ParseXML = function (url) {
    var itemList = null;

    $.ajax({
        type: "GET",
        url: url,
        dataType: "xml",
        async: false,
        success: function(xml) {
            $(xml).find("event").each(function() {
                var _id = $(this).attr("id");
                var _eventItemDay = $(this).find("eventItemDay").text();
                ...
                var _eventItemLocation = $(this).find("eventItemLocation").text();

                itemList = {
                    id: _id,
                    eventItemDay: _eventItemDay,
                    eventItemLocation: _eventItemLocation,
                    ...
                    eventItemLocation: _eventItemLocation
                };
            });

        },
        error: function(xhr, ajaxOptions, thrownError){
            alert("XML ERROR");
            alert(xhr.status);
            alert(thrownError);
        }
    });

    return itemList;
};
于 2013-06-27T14:35:48.647 回答
0

您可能需要考虑使您的 ajax 调用异步并向 ParseXML 函数添加回调。事件处理程序中的类似内容:

itemList = Controller.ParseXML("app/data/Event.xml", function(itemList){
    alert("itemList = " + itemList);
});

在 ParseXML 中:

Controller.ParseXML = function (url, callback) {
var itemList = null;

$.ajax({
    type: "GET",
    url: url,
    dataType: "xml",
    async: true,
    success: function(xml) {
        $(xml).find("event").each(function() {
            var _id = $(this).attr("id");
            var _eventItemDay = $(this).find("eventItemDay").text();
            ...
            var _eventItemLocation = $(this).find("eventItemLocation").text();

            itemList = {
                id: _id,
                eventItemDay: _eventItemDay,
                eventItemLocation: _eventItemLocation,
                ...
                eventItemLocation: _eventItemLocation
            };
            callback( itemList );
        });
    },
    error: function(xhr, ajaxOptions, thrownError){
        alert("XML ERROR");
        alert(xhr.status);
        alert(thrownError);
        callback( "XML ERROR " + xhr.status + " " + thrownError );
    }
});  

};

当然,您需要检查回调中的值是否存在错误。

于 2013-06-27T14:57:29.660 回答