Skip to content

Instantly share code, notes, and snippets.

@bgadrian
Created March 16, 2015 17:20
Show Gist options
  • Save bgadrian/e70e8ba1df48b373e958 to your computer and use it in GitHub Desktop.
Save bgadrian/e70e8ba1df48b373e958 to your computer and use it in GitHub Desktop.
JS jquery object plugin template
//thanks to http://www.virgentech.com/blog/2009/10/building-object-oriented-jquery-plugin.html
(function($){
var MyPlugin = function(element, options)
{
var elem = $(element);
var obj = this;
var settings = $.extend({
param: 'defaultValue'
}, options || {});
// Public method - can be called from client code
this.publicMethod = function()
{
console.log('public method called!');
};
// Private method - can only be called from within this object
var privateMethod = function()
{
console.log('private method called!');
};
};
$.fn.myplugin = function(options)
{
return this.each(function()
{
var element = $(this);
// Return early if this element already has a plugin instance
if (element.data('myplugin')) return;
// pass options to plugin constructor
var myplugin = new MyPlugin(this, options);
// Store plugin object in this element's data
element.data('myplugin', myplugin);
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment