Last active
September 23, 2015 22:48
-
-
Save keeganwatkins/628749 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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