Skip to content

Instantly share code, notes, and snippets.

@micmath
Created March 11, 2011 11:32
Show Gist options
  • Save micmath/865775 to your computer and use it in GitHub Desktop.
Save micmath/865775 to your computer and use it in GitHub Desktop.
Wrapping a plugin that modifies the jQuery object in a "sandbox" so that we can control exactly which jQuery the plugin modifies.
define('coolplug-1', function() {
// sandbox function
return function($) {
//---- usual plugin code starts here ----
$.fn.cool = function() { alert('cool!'); }
//---- usual plugin code ends here ------
return $;
}
});
define('gelApp', ['jquery-1', 'coolplug-1']
function($, plugin) {
// $ is a reference to the same jquery-1 everyone gets from requireJS
var plugged$ = plugin( $.sub() ); // attach plugin to a local copy of $
plugged$('p').cool(); // <-- alerts 'cool!'
$('p').cool(); // <-- Error: "cool" undefined
}
);
@yavor-atanasov
Copy link

so if we someone wants to use jquery with three plugins extending just his "private copy" of jquery:

define('gelApp', ['jquery-1', 'coolplug-1', 'coolplug-2', 'coolplug-3'],
function($, plugin1, plugin2, plugin3) {

    // $ is overwritten and is now a completely separate local copy with the added plugins

    $ = plugin3(plugin2(plugin1( $.sub() ))); 

}

);

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