Last active
April 16, 2016 21:15
-
-
Save kristian/808d7074c79de5c777f5d9b2e493ce2e to your computer and use it in GitHub Desktop.
jQuery Ajax Queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* jQuery.ajaxQueue - Named queues for ajax requests | |
* | |
* (c) 2016 Kristian Kraljic, 2012 Corey Frang | |
* Dual licensed under the MIT and GPL licenses. | |
* | |
* Requires jQuery 1.5+, jQuery.ajaxQueue function, use with (jQuery.ajaxQueue||jQuery.ajax)(sUrl,oOptions,sQueueName).done(function(oData) { ... } ).fail(function() { ... } ); | |
*/ | |
(function(jQuery) { | |
var mAjaxQueues = {}; // map for all ajaxQueues we are going to create | |
jQuery.ajaxQueue = function(sUrl, oOptions, sQueueName) { | |
if(typeof sUrl==='object') { | |
sQueueName = oOptions; | |
oOptions = sUrl; | |
sUrl = undefined; | |
} | |
if(typeof oOptions==='string') { | |
sQueueName = oOptions; | |
oOptions = undefined; | |
} | |
// Force options to be an object, use default queue if no queue i given | |
oOptions = oOptions||{}; | |
sQueueName = sQueueName||'default'; | |
// jQuery on an empty object, we are going to use this as our queue | |
var oAjaxQueue = mAjaxQueues[sQueueName]||(mAjaxQueues[sQueueName]=jQuery({})); | |
var oXHR, oDeferred = jQuery.Deferred(), oPromise = oDeferred.promise(); | |
function fnRequest(oNext) { (oXHR=jQuery.ajax(sUrl,oOptions)) | |
.done(oDeferred.resolve).fail(oDeferred.reject).then(oNext,oNext); } | |
oAjaxQueue.queue(fnRequest); | |
// Add the abort method to the promise | |
oPromise.abort = function(sStatusText) { | |
if(oXHR) return oXHR.abort(sStatusText); | |
// if there wasn't already a jqXHR we need to remove from queue | |
var aQueue = oAjaxQueue.queue(), iIndex = jQuery.inArray(fnRequest, aQueue); | |
if(iIndex>-1) aQueue.splice(index, 1); | |
oDeferred.rejectWith(oOptions.context||oOptions, [oPromise,sStatusText,'']); | |
return oPromise; | |
}; | |
return oPromise; | |
}; | |
})(jQuery); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* jQuery.ajaxQueue - Named queues for ajax requests | |
* | |
* (c) 2016 Kristian Kraljic, 2012 Corey Frang | |
* Dual licensed under the MIT and GPL licenses. | |
* | |
* Requires jQuery 1.5+, jQuery.ajaxQueue function, use with (jQuery.ajaxQueue||jQuery.ajax)(sUrl,oOptions,sQueueName).done(function(oData) { ... } ).fail(function() { ... } ); | |
*/ | |
!function(e){var r={};e.ajaxQueue=function(t,n,o){function u(r){(i=e.ajax(t,n)).done(f.resolve).fail(f.reject).then(r,r)}"object"==typeof t&&(o=n,n=t,t=void 0),"string"==typeof n&&(o=n,n=void 0),n=n||{},o=o||"default";var i,a=r[o]||(r[o]=e({})),f=e.Deferred(),c=f.promise();return a.queue(u),c.abort=function(r){if(i)return i.abort(r);var t=a.queue(),o=e.inArray(u,t);return o>-1&&t.splice(index,1),f.rejectWith(n.context||n,[c,r,""]),c},c}}(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment