Skip to content

Instantly share code, notes, and snippets.

@jrburke
Created November 8, 2012 05:55
Show Gist options
  • Save jrburke/4037081 to your computer and use it in GitHub Desktop.
Save jrburke/4037081 to your computer and use it in GitHub Desktop.
RequireJS jQuery adapter approach
//Set up the adapter config, so that any module asking
//for jquery gets an adapter module that has modified
//jquery before other modules can use it.
//Needs RequireJS 2.0+ to work correctly
//More info on map config:
//http://requirejs.org/docs/api.html#config-map
requirejs.config({
map: {
'*': {
'jquery': 'jquery.adapter'
},
'jquery.adapter': {
'jquery': 'jquery'
}
}
});
define(['jquery'], function ($) {
//Modify jQuery here.
//If you wanted, this module can
//also list some jquery plugins to have them already
//attached to the jquery object. However, if you do
//that, then you need to set up the requirejs config
//to either specify a map config for those plugins so
//that they get the real jquery, or use shim config that
//specifies 'jquery.adapter' as a dependency instead of 'jquery',
//if the jquery plugins do not call define() themselves.
//Return jQuery to set this module value;
return $;
});
@millermedeiros
Copy link

you can use the same pattern to avoid exposing globals:

// jquery.adapter.js
define(['jquery'], function($){
   // avoid exposing any globals
   return $.noConflict(true);
});

been doing it on almost all projects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment