Return to Snippet

Revision: 52785
at November 1, 2011 15:08 by jackkeller


Initial Code
// A boilerplate for kick-starting jQuery plugins development
// version 1.2, April 29th, 2011
// by Stefan Gabos
// with help from Steven Black, Rob Lifford

(function($) {

    $.fn.pluginName = function(method) {

        var defaults = {
            foo: 'bar'
        }

        var settings = {}

        var methods = {

            init : function(options) {
                settings = $.extend({}, defaults, options)
                return this.each(function() {
                    var
                        $element = $(this),
                        element = this;
                    // code goes here
                });
            },

            foo_public_method: function() {
                // code goes here
            }

        }

        var helpers = {

            foo_private_method: function() {
                // code goes here
            }

        }

        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 in pluginName plugin!');
        }

    }

})(jQuery);

Initial URL


Initial Description


Initial Title
jQuery Plugin Boilerplate

Initial Tags
jquery

Initial Language
JavaScript