$.createPlugin

This script defines a new jQuery method called "createPlugin", used to easily create new jQuery plugins within the "$.fn" plugin scope.

by Helmut Granda

HTML

<div id="test1"></div>
<div id="test2" class="myShit"></div>
<div id="test3" class="myShit"></div>

CSS

div {
    margin-bottom: 15px;
    font-family: verdana;
    font-size: 10px;
}

JavaScript

/*!
 * $.createPlugin(pluginName, pluginImplementation)
 *
 * jQuery Plugin Creation Method (boilerplate plugin implementations)
 * This script defines a new jQuery method called "createPlugin",
 * used to easily create new jQuery plugins within the "$.fn" plugin scope.
 *
 * This method returns nothing, and receives two required parameters:
 *   [string] pluginName: the unique plugin name.
 *   [object] pluginImpl: the object containing the plugin implementation.
 *
 * The method tries to easily and clearly decouple the code used to implement
 * the business logic (as of the plugin implementation) from the code required
 * to implement some of the jQuery's best practices when creating new jQuery plugins.
 *
 * ====== Sample plugin implementation object: ======
 *
 * // Creates all the business logic of your plugin (aka your plugin implementation object)
 * var testPluginCode = {
 *     _defaults: {}, // you can put your plugin defaults in here.
 *
 *     init: function() {
 *         // plugin initialization goes here.
 *         // this method is called just once per instance.
 *         // you can use this.element to access the HTML element on which the plugin was called at.
 *         // this.$element is the same as $(this.element).
 *         // this.options represents the options object passed in during plugin initialization, merged with the _defaults value defined for your plugin.
 *         // this._name represents the name on which the plugin was registered at the $.fn scope.
 *     },
 *
 *     doSomething: function(paramA, paramB) {
 *         // plugin business logic for method "doSomething".
 *     }
 * };
 * 
 * // Creates your plugin, which will be called "myPlugin",
 * // using the "testPluginCode" object as the plugin implementation code.
 * (function($){
 *     $.createPlugin('myPlugin', testPluginCode);
 * }(jQuery));
 *
 * // Now, you can use your plugin the same way you'd use any other jQuery plugin:
 *
 * var myDiv = $('#testDiv');          ...