Skip to content

Instantly share code, notes, and snippets.

@s2b
Created October 9, 2013 12:59
Show Gist options
  • Save s2b/6900845 to your computer and use it in GitHub Desktop.
Save s2b/6900845 to your computer and use it in GitHub Desktop.
Require.js plugin to detect the global jQuery version and use a local jQuery if the global version is too old.
// Map jquery module to the plugin
// Credits to jburke
// https://github.com/jrburke/requirejs/issues/451#issuecomment-8442745
require.config({
map: {
'*': {
'jquery': 'jquery!'
}
}
});
define(["versionCompare"], function (versionCompare) {
// Specify the required jQuery version here
var jQueryVersion = '1.10';
return {
load: function (name, parentRequire, onLoad, config) {
if ($ in window && versionCompare(window.$.fn.jquery, jQueryVersion, '>=')) {
// Return global jQuery version
onLoad(window.$);
} else {
// Load and use local jQuery version (of course jquery-ui can be omitted here)
parentRequire(["lib/jquery", "lib/jquery-ui"], function (jQuery) {
// No conflict with existing jQuery
jQuery.noConflict();
// More jQuery initialization (like event proxying between the jQuery versions)
// can be put here
// window.$: global jQuery
// jQuery: local jQuery
// Return local jQuery
onLoad(jQuery);
});
}
}
};
});
define(function () {
// versionCompare module could return a function like this:
// http://phpjs.org/functions/version_compare/
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment