Skip to content

Commit

Permalink
add ability to call public prototype functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeno Rocha committed May 13, 2012
1 parent d6564ce commit 94acc33
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions patterns/jquery.basic.plugin-boilerplate.js
Expand Up @@ -62,15 +62,27 @@
}
};

// A really lightweight plugin wrapper around the constructor,
// preventing against multiple instantiations
// A really lightweight plugin wrapper around the constructor,
// preventing against multiple instantiations and allowing any
// public function (ie. a function whose name doesn't start
// with an underscore) to be called via the jQuery plugin,
// e.g. $(element).defaultPluginName('functionName', arg1, arg2)
$.fn[pluginName] = function ( options ) {
return this.each(function () {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName,
new Plugin( this, options ));
}
});
var args = arguments;
if (options === undefined || typeof options === 'object') {
return this.each(function () {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName, new Plugin( this, options ));
}
});
} else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') {
return this.each(function () {
var instance = $.data(this, 'plugin_' + pluginName);
if (instance instanceof Plugin && typeof instance[options] === 'function') {
instance[options].apply( instance, Array.prototype.slice.call( args, 1 ) );
}
});
}
}

})( jQuery, window, document );

0 comments on commit 94acc33

Please sign in to comment.