Skip to content

Instantly share code, notes, and snippets.

@messutied
Last active August 29, 2015 14:02
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 messutied/d161f660d6509f2b4709 to your computer and use it in GitHub Desktop.
Save messutied/d161f660d6509f2b4709 to your computer and use it in GitHub Desktop.
jQuery Plugin Boilerplate
(function( $ ) {
var settings = {};
// private methods
var somePrivateMethod = function() {
}
// public methods
var methods = {
init: function(options) {
var self = this;
var $this = $(this);
settings = $.extend({
defaultOption1: true,
defaultOption2: false
}, options);
$this.each(function () {
var $element = $(this);
});
},
someMethod: function(arg1) {
}
};
$.fn.basicPluginName = function(method) {
// Method calling logic
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.zenti.basicPluginName' );
}
};
})( jQuery );
$(function() {
// initializing
$("#someElementsId").basicPluginName({options1: true});
// calling a method
$("#someElementsId").basicPluginName("someMethod", 4);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment