Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created January 31, 2011 21:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cowboy/804811 to your computer and use it in GitHub Desktop.
Save cowboy/804811 to your computer and use it in GitHub Desktop.
jQuery.sub() "module-specific jQuery extensions" usage example for jQuery 1.5
// Your module.
(function($uper){
var $ = $uper.sub();
// This method ONLY exists inside this IIFE.
$.fn.remove = function() {
if ( confirm( "Really remove " + this.length + " elements?" ) ) {
// Execute (and return the value of) the main jQuery .remove method.
return $uper.fn.remove.apply( this, arguments );
} else {
// Just return the collection of elements.
return this;
}
};
// This method exists everywhere, because it is defined on the "super" jQuery.
$uper.fn.alertLength = function() {
alert( "There are " + this.length + " selected elements!" );
return this;
};
$(function(){
// Annoying alert!
$("ul").alertLength();
// Requests confirmation before removing!
$("ul:first").remove();
});
})(jQuery);
// Some other guy's module.
(function($){
$(function(){
// Annoying alert!
$("ul").alertLength();
// Removes normally, no confirmation dialog.
$("ul:first").remove();
// Annoying alert!
$("ul").alertLength();
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment