Code Monkey home page Code Monkey logo

jquery.makeplugin.js's Introduction

"Yo dawg, I heard you like jQuery plugins..."

About

jquery.makePlugin.js is a jQuery function that builds jQuery plugins. It eliminates the need for all of the boilerplate code required to build a solid jQuery plugin, and instead lets you focus on developing your plugin's functionality.

jquery.makePlugin.js gives your plugin support for managing event handlers (bind, unbind, trigger), plugin destruction (automatic unbinding of event handlers, per the recommendation), and public API methods, all automatically.

Usage

$.makePlugin({
    "pluginName": "PLUGIN_NAME",

    "defaults": {
        // Default values...
    },

    "init": function () {
        // Plugin initialization code...
    },

    "api": {
        // Public API methods...
    },

    "helpers": {
        // Private helper methods/properties...
    },

    "eventHandlers": [
        // Event handlers...
    ]
});
Breakdown

$.makePlugin({ ...

Creates a new jQuery plugin by calling the makePlugin function defined in jquery.makePlugin.js.


"pluginName": "PLUGIN_NAME", ...

Specifies the name of the plugin, and, ipso facto, the name of the function used to construct the plugin, eg., "myPlugin" -> $("#foo").myPlugin().


"defaults": {
    // Default values...
}, ...

An object specifying default values for the plugin. These values can be overridden when the plugin is constructed. Prior to the init method being run, the caller's overriding values are merged with the plugin defaults; the resulting object is available to all plugin methods (public and private) via this.config.


"init": function () {
    // Plugin initialization code...
}, ...

The plugin's initialization method. This method is run after the default plugin initialization so that plugin authors can use API and helper methods/properties during init. The this context for init is the plugin instance. This method is not callable from other plugin methods.


"api": {
    // Public API methods...
},  ...

The plugin's publicly-visible (API) methods. Methods defined here are available internally via this.api.<method>, and externally via $("#foo").pluginName("method", arg1, argN...). The this context for all API methods is the plugin instance.


"helpers": {
    // Private helper methods/properties...
}, ...

The plugin's internally-visible (private) methods and properties. Members defined here are available internally via this.helpers.<method|property>. The this context for all helper methods is the plugin instance.


"eventHandlers": [
    // Event handlers...
] ...

An array of event handler objects that will be attached during initialization. Event handlers can be attached to plugin elements, or to external elements (such as window, or another DOM element). Alternatively, event handlers can be registered by calling this.bind(), and unregistered with this.unbind(). Event handlers registered in this way (either as part of the plugin definition, or by calling this.bind()) can be manually triggered by calling this.trigger({ "eventName": "click" }).

Example:

// Attach a handler to the plugin element's "click" event
{
    "eventName": "click",
    "handler": function (e) {
        // Change the element's background color
        var el = this.element;
        $(el).css("backgroundColor", "#cfa");
        alert("You clicked me!");
    }
},

// Attach a handler to the window object's "resize" event
{
    "target": window,
    "eventName": "resize",
    "handler": function (e) {
        console.info("The window is resizing!");
    }
}

jquery.makeplugin.js's People

Contributors

matthewaburton avatar

Watchers

 avatar

jquery.makeplugin.js's Issues

Add dependency checking mechanism

Plugin authors need the ability to check for other jQuery plugins, JavaScript libraries, and (possibly) other libraries such as Twitter Bootstrap before their plugin is initialized. Currently, authors must perform these checks in the plugin's init method, which clumps together dependency checking code and actual initialization code, potentially leading to a cluttered and difficult-to-read init method.

Since the init method should never get called if the plugin's dependencies are not loaded, it makes sense to have a separate dependency checking function which runs prior to plugin initialization. The functionality should be straightforward, and it should be flexible enough for authors to check for any type of dependency they need.

Add support for delayed event handlers

Allow plugin authors to declare that an event handler should only be run after a specified span of time has elapsed.

An example usage of this feature is a plugin author delaying the transmission of a search-as-you-type Ajax call until the user has stopped typing for more than 300 milliseconds.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.