Last active
August 29, 2015 14:02
-
-
Save messutied/d161f660d6509f2b4709 to your computer and use it in GitHub Desktop.
jQuery Plugin Boilerplate
This file contains hidden or 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
(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 ); |
This file contains hidden or 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
$(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