Skip to content

Instantly share code, notes, and snippets.

@gcoop
Created October 25, 2010 10:41
Show Gist options
  • Save gcoop/644756 to your computer and use it in GitHub Desktop.
Save gcoop/644756 to your computer and use it in GitHub Desktop.
jQuery Plugin Template Code
(function (window, document, $, undefined) {
var dataKey = 'MyPlugin';
var methods = {
init: function (opts) {
var defaults = {
'offset': 0
}, o = $.extend(defaults, opts);
return this.each(function () {
var $this = $(this),
data = $(this).data(dataKey);
if (!data) {
// Setup
$this.data(dataKey, {
offset: o.offset,
visible: false
});
}
});
}
}
$.fn.MyPlugin = function (method) {
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.tooltip' );
}
}
})(this, this.document, jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment