Return to Snippet

Revision: 45216
at April 27, 2011 14:22 by Japh


Initial Code
//
//  A jQuery plugin template
//
//  Author: Stefan Gabos
//
//  Website: http://stefangabos.ro/
//
//  Changelog
//
//  2011-04-25  1.0 -   Initial release
//
//  Read more about jQuery plugin authoring at
//  http://docs.jquery.com/Plugins/Authoring
//


// remember to change every instance of "pluginName" to the name of your plugin!
// read more about plugin authoring at
(function($) {

    // here it goes!
    $.fn.pluginName = function(method) {

        // plugin's default options
        var options = {

            foo: 'bar'

        }

        // public methods
        // to keep the $.fn namespace uncluttered, collect all of the plugin's methods in an object literal and call them
        // by passing the string name of the method to the plugin
        //
        // public methods can be called as
        // $(selector).pluginName('methodName', arg1, arg2, ... argn)
        // where "pluginName" is the name of your plugin and "methodName" is the name of a function available in the
        // "methods" object below; arg1 ... argn are arguments to be passed to the method
        //
        // or, from within the plugin itself, as
        // methods.methodName(arg1, arg2, ... argn)
        // where "methodName" is the name of a function available in the "methods" object below
        var methods = {

            // this the constructor method that gets called when the object is created
            init : function(config) {

                // iterate through all the DOM elements we are attaching the plugin to
                return this.each(function() {

                    // if custom configuration options are provided, update default values
                    if (config) { $.extend(options, config) }

                    // "element" holds the jQuery object of the current DOM element
                    var element = $(this);

                    // code goes here

                });

            },

            // a public method. for demonstration purposes only - remove it!
            foo_public_method: function() {

                // code goes here

            }

        }

        // private methods
        // these methods can be called only from within the plugin
        //
        // private methods can be called as
        // helpers.methodName(arg1, arg2, ... argn)
        // where "methodName" is the name of a function available in the "helpers" object below; arg1 ... argn are
        // arguments to be passed to the method
        var helpers = {

            // a private method. for demonstration purposes only - remove it!
            foo_private_method: function() {

                // code goes here

            }

        }

        // if a method as the given argument exists
        // (ignore the "init" method which should not be called manually)
        if (methods[method] && method.toLowerCase() != 'init') {

            // call the respective method
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));

        // if an object is given as method OR nothing is given as argument
        } else if (typeof method === 'object' || !method) {

            // call the initialization method
            return methods.init.apply(this, arguments);

        // otherwise
        } else {

            // trigger an error
            $.error( 'Method "' +  method + '" does not exist in PluginName plugin!');

        }

    }

})(jQuery);

Initial URL
http://stefangabos.ro/jquery/jquery-plugin-boilerplate/

Initial Description
This plugin is the work of Stefan Gabos, all credit, rights, and praise to be directed to him here: http://stefangabos.ro/jquery/jquery-plugin-boilerplate/

Initial Title
jQuery Plugin Boilerplate

Initial Tags
plugin, jquery

Initial Language
jQuery