如何在ajax完成之前停止对象方法调用

时间:2011-07-21 04:35:02

标签: javascript jquery ajax

我有以下java脚本对象

function eventTypeObj() {

allEventTypes = [];

// When the object is created go and get all the event types that can be included in journey or clusters.
$.ajax({
        url: "/ATOMWebService.svc/GetDisplayEventTypes",
        dataType: "json",
        success: function(result) {
            allEventTypes = eval("(" + result.d + ")");
        }
    });


// Returns a list of all the event type IDS.
this.getEventTypeIds = function() {
    var eventTypeIDs = [];
    for (var i = 0; i < allEventTypes.length; i++) {
        eventTypeIDs.push(allEventTypes[i].Id);
    }
    return eventTypeIDs;
};
}

我想知道是否有办法阻止某人调用eventTypeObj.getEventTypeIds();在构造函数中的ajax调用成功之前,allEventTypes数组中没有数据?

4 个答案:

答案 0 :(得分:2)

像这样的东西会更好(我不保证这是100%工作,但概念是合理的):

function eventTypeObj() {
    this.allEventTypes = [];
    this.hasLoadedEventTypes = false;

    var loadEventTypes = function(cb) {
        $.ajax({
            url: "/ATOMWebService.svc/GetDisplayEventTypes",
            dataType: "json",
            success: function(result) {
                this.allEventTypes = eval("(" + result.d + ")");
                this.hasLoadedEventTypes = true;
                cb();
            }
        });
    };


    this.getEventTypeIds = function(updateEventTypes, callback) {
        var _getEventTypeIds = function() {
            var eventTypeIDs = [];
            for (var i = 0; i < this.allEventTypes.length; i++) {
                eventTypeIDs.push(this.allEventTypes[i].Id);
            }    
            return eventTypeIDs;
        };


        if (!this.hasLoadedEventTypes || updateEventTypes) {
            loadEventTypes(function(){ callback(_getEventTypeIds()); });
        }
        else callback(_getEventTypeIds());
    };
}

使用示例:

var eto = new eventTypeObj();

eto.getEventTypeIds(false, function(eventTypeIdArray) {
   // do stuff with the id array 
});


/*
    somewhere later on you want to get an updated eventTypeId array
    in case the event types have changed.
*/
eto.getEventTypeIds(true, function(eventTypeIdArray) {
    // do stuff with the updated ids
});

答案 1 :(得分:1)

没有办法阻止某人过早打电话。如果他们过早地打电话,你会想要发生什么?

如果还没有填写allEventTypes,那么现在看起来你的代码当前返回一个空数组。你可以决定空数组是否是正确的结果,或者如果你太早调用它就应该抛出异常来制作它呼叫者绝对清楚数据尚不可用。

您可以为需要该信息的人提供一些帮助代码,但可能尚未提供。例如,您可以允许它们注册一个回调,该回调将在数据填写后从成功处理程序中调用。您可以允许它们查询数据是否可用。

如果您不希望时间对呼叫者负责,那么您无法提供同步方式来获取此信息。相反,您只会提供一个用于获取数据的回调机制。如果数据准备就绪,则会立即调用回调。如果数据尚未就绪,则在ajax函数完成时将调用回调。在任何一种情况下,调用者都必须只处理回调中的数据,并且getEventTypeIds不是正常的调用来获取现在的数据,而是调用注册将在数据时调用的回调。准备。这样可以减轻调用者必须知道数据何时准备好的实现细节,但会迫使他们使用回调机制的异步特性。

this.getEventTypeIds = function(callback) {
    if (allEventTypes.length > 0) {
        // data is ready call the callback with the data now
    } else {
        // store the callback to be called later from the success handler
    }
}

答案 2 :(得分:1)

var allowCall = false;
function eventTypeObj() {

allEventTypes = [];

// When the object is created go and get all the event types that can be included in journey or clusters.
$.ajax({
        url: "/ATOMWebService.svc/GetDisplayEventTypes",
        dataType: "json",
        success: function(result) {
            allEventTypes = eval("(" + result.d + ")");
            allowCall = true;
        }
    });


// Returns a list of all the event type IDS.
this.getEventTypeIds = function() {
    if(!allowCall) return;  // or pop up a message
    var eventTypeIDs = [];
    for (var i = 0; i < allEventTypes.length; i++) {
        eventTypeIDs.push(allEventTypes[i].Id);
    }
    return eventTypeIDs;
};
}

或者只检查allEventTypes是否为空。

答案 3 :(得分:0)

您可以检查eventType数组是否为空,对吗?

if(allEventTypes.length == 0)
{
    return;
}