Skip to content

Instantly share code, notes, and snippets.

@keeganwatkins
Last active September 23, 2015 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keeganwatkins/628749 to your computer and use it in GitHub Desktop.
Save keeganwatkins/628749 to your computer and use it in GitHub Desktop.
// jQuery UI sugar: an easy way to limit a plugin generated by the widget
// factory down to the first element. This is useful for widgets like dialog
// and mediaplayer that should be instantiated/configured individually.
$.widget.singularize = function(widgetName) {
// Keep a reference to the auto-generated plugin, so that we can use it later
var widget = $.fn[ widgetName ],
slice = Array.prototype.slice;
// Abort is widget doesn't exist to avoid creating random properties on $.fn
if (!widget || !($.isFunction(widget))) {
return;
}
// Re-define plugin
$.fn[ widgetName ] = function() {
// Only keep the first element in the set
var self = this.eq( 0 ),
// Normalize arguments object to true array
args = slice.call( arguments );
// Invoke the original plugin.
return widget.apply( self, args );
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment