Last active
December 12, 2015 01:19
-
-
Save JimBobSquarePants/4690594 to your computer and use it in GitHub Desktop.
jQuery plugins to get multiple scripts in a single call. In cached and un-cached flavours.
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
(function ($) { | |
/** | |
* Multiple parallel getScript with caching. | |
* | |
* @param {Array|String} url (one or more URLs) | |
* @param callback fn (oncomplete, optional) | |
* @returns {function} | |
*/ | |
$.getCachedScript = function (url, callback) { | |
// Normalise type | |
if (!$.isArray(url)) { | |
url = [url]; | |
} | |
$.when.apply(null, $.map(url, function (val) { | |
$.ajax({ | |
dataType: "script", | |
cache: true, | |
url: val | |
}); | |
})).done(function () { | |
if (callback && typeof callback === "function") { | |
callback(); | |
} | |
}); | |
}; | |
}(jQuery)); | |
(function ($) { | |
// Save the original version. | |
var getScript = $.getScript; | |
/** | |
* Multiple parallel getScript. | |
* | |
* @param {Array|String} url (one or more URLs) | |
* @param callback fn (oncomplete, optional) | |
* @returns {function} | |
*/ | |
$.getScript = function (url, callback) { | |
// Normalise type | |
if (!$.isArray(url)) { | |
url = [url]; | |
} | |
$.when.apply(null, $.map(url, getScript)).done(function () { | |
if (callback && typeof callback === "function") { | |
callback(); | |
} | |
}); | |
}; | |
}(jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment