-
-
Save geminiyellow/801abae34c3fecec6d96 to your computer and use it in GitHub Desktop.
xhrPool - extendable async xml requests pool, with sequent callbacks per xhr
This file contains hidden or 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
// xhrPool - async xml requests pool, with sequent callbacks per xhr | |
// | |
$.xhrPool = []; | |
$.xhrPool.callbacks = []; | |
$.xhrPool.starthandlers = []; | |
$.xhrPool.onEnd = function (name, obj, opts) { | |
$.xhrPool = $.extend($.xhrPool, obj); | |
$.xhrPool.callbacks.push({ 'fn': name, 'opts': opts }); | |
}; | |
$.xhrPool.onBegin = function (name, obj, opts) { | |
$.xhrPool = $.extend($.xhrPool, obj); | |
$.xhrPool.starthandlers.push({ 'fn': name, 'opts': opts }); | |
}; | |
$.xhrPool.flush = function () { | |
$.xhrPool.callbacks = []; | |
$.xhrPool.starthandlers = []; | |
}; | |
$.xhrPool.abortAll = function () { | |
$(this).each(function (idx, jqXhr) { | |
jqXhr.abort(); | |
}); | |
$.xhrPool.length = 0; | |
}; | |
//usage | |
if (!$.xhrPool.OnException) | |
$.xhrPool.onEnd('OnException', { 'OnException': function (jx, opts, pool) { | |
if (jx.statusText == "abort") { | |
$.xhrPool.empty(); | |
jx.exception = 'true'; | |
jx.error = function () { }; | |
jx.success = function () { }; | |
jx = null; | |
} | |
} | |
}, null); | |
if (!$.xhrPool.StopLoadingSpinner) | |
$.xhrPool.onEnd('StopLoadingSpinner', { 'StopLoadingSpinner': function (jx, opts, pool) { | |
// $("#content").spin(false); | |
} | |
}, null); | |
if (!$.xhrPool.StartLoadingSpinner) | |
$.xhrPool.onBegin('StartLoadingSpinner', { 'StartLoadingSpinner': function (jx, opts, pool) { | |
// $("#content").spin("ajaxload"); | |
} | |
}, null); | |
$.ajaxSetup({ | |
beforeSend: function (jqXhr) { | |
if (!$.xhrPool) return; | |
$.xhrPool.push(jqXhr); | |
$($.xhrPool.starthandlers).each(function (e) { | |
var fn = $.xhrPool.starthandlers[e].fn; | |
var opts = $.xhrPool.starthandlers[e].opts; | |
if (fn) { | |
var func = $.xhrPool[fn]; | |
if (typeof (func) == 'function') func(jqXhr, $.extend(opts, $.xhrPool)); | |
} | |
}); | |
} | |
}); | |
$(document).ajaxComplete(function (jxhr) { | |
if (!$.xhrPool) return; | |
var index = $.xhrPool.indexOf(jxhr); | |
if (index > -1) { | |
$.xhrPool.splice(index, 1); | |
} | |
$($.xhrPool.callbacks).each(function (e) { | |
var fn = $.xhrPool.callbacks[e].fn; | |
var opts = $.xhrPool.callbacks[e].opts; | |
if (fn) { | |
var func = $.xhrPool[fn]; | |
if (typeof (func) == 'function') func(jxhr, $.extend(opts, $.xhrPool)); | |
} | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment