user1684699
user1684699

Reputation: 77

Javascript: $.post not $.ajax queuing script

I'm using the ajaxQ queuing script for my post requests I'm making, but the $.ajax function is a little messy and i don't need any of the extra options that it offers over $.post. Anyone know of a queuing system for $.post requests?

I want to turn this:

        $.ajaxq('queue', { 

        type: 'POST', 
        url: baseURL + '/ChangeItem/Check', 
        dataType: 'text',
        data: { 
            'newItem': item, 
            'purchaseItem': false
        },
        error: function(jqXHR, textStatus) {
            alert("Error: " + textStatus); 
        },
        success: function(data) {
            if(thisObject.isNotTaken(data)) thisObject.claimItem(item);  
                else thisObject.proccessResults(list, index+1);
        }
    });

Into something short and clean like this:

$.post(baseURL + '/ChangeItem/Check, { 'newItem': item, 'purchaseItem': false }, 
     function(data) { /* Success function here */ });

Upvotes: 0

Views: 134

Answers (1)

Crozin
Crozin

Reputation: 44386

You could create your own $.postq()/$.getq() methods which will act as shortcuts for $.ajaxq(). Just like $.post() does for $.ajax(): https://github.com/jquery/jquery/blob/master/src/ajax.js#L223

jQuery.each(["getq", "postq"], function(i, method) {
    jQuery[method] = function(url, data, callback, type) {
        // shift arguments if data argument was omitted
        if (jQuery.isFunction(data)) {
            type = type || callback;
            callback = data;
            data = undefined;
        }

        return jQuery.ajaxq({
            type: method,
            url: url,
            data: data,
            success: callback,
            dataType: type
        });
    };
});

Upvotes: 1

Related Questions