• Home
  • About
  • Links
  • Contact
  • Imprint
Blue Orange Green Pink Purple

How to develop a jQuery plugin

Posted in javascript, jquery. on Sunday, November 15th, 2009 by Chris Tags: javascript, jquery
Nov 15

Lately I started a new project at a German ISP where we (the team and me) have to develop several extensions to jQuery in order to build a framework to work with in this project. I am using jQuery for several years now but never had to develop my own extensions, because most of the plugins available solved problems I came a cross. This time I have to do my own and started questioning uncle google about how to do it. No surprise there are several approaches to do a plugin for jQuery, but none of the tutorials I found answered all my Questions completely, so I decided to write this sum up of what I learned.

First of all you will need the basic wrapping:

(function($){
   // your js code goes here
})(jQuery);

This wrapping adds your code to the jQuery library if you add the js file to your page.
Next thing you will have to do is add the basic functionality to your plugin.

$.fn.yourPlugin = function(options){
}

By adding those lines to your js file within the first code snippet you now can access your plugin by:

$('#yourDomElement').yourPlugin();

Next you need to know the difference between private and public vars within the class you are developing. If you write something like:

   /**
     * default options for plugin
     * private
     * @var array defaultOptions
     */
    var defaultOptions = {
        action:       false,
        debug:        false
    }

This would be a private var witch is only accessible within the class it self. If you need to have a var of your class to be public you have to write something like this:

   /**
     * default options for plugin
     * public
     * @var array defaultOptions
     */
    $.fn.yourPlugin.defaultOptions = {
        action:       false,
        debug:        false
    }

One word has to be said by now, public variables can only be defined after the declaration of the constructor of your plugin, otherwise the compiler will throw a nice little javascript error because “yourPlugin” was not defined yet.
Now that we have learned this we can go on with the possibility of configuration for the plugin. Like in every other language we will need to have some basic properties for the class that can be overwritten by your needs. To do something like that is quite simple:

   /**
     * yourPlugin base constructor
     *
     * @param [] options
     * @return void
     */
    $.fn.yourPlugin = function(options){
        // extend default options with overrides
        var opts = $.extend({}, defaultOptions, options);
    }

So by now you can set the options you need by writing something like this:

   $('#yourDomElement').yourPlugin({
      action:       myAction
    });

In this case you would have set opts.action from “false” to “myAction”.
Next we will go to private and public class methods. It is allmost the same as defining public and private member variables of your class.
Something like this would be private:

   /**
     * private
     * method to do some vodoo
     */
    function yourPrivateClassMethod()
    {
        // the methods code
    }

And something like this is a public method:

   /**
     * public
     * method to do some vodoo
     */
    $.fn.yourPlugin.yourPublicClassMethod = function()
    {
        // the methods code
    }

Same rules like for the public member variables,… they have to be declared after your constructor, otherwise you will get an error. Another word of advise,.. as a developer I thought to access variables and methods within a class I can go by “this”,.. forget “this”,.. private methods and vars are accessed directly by their name, the public one you can access either by $(this).yourPlugin.yourPublicClassMethod() or $.fn.yourPlugin.yourPublicClassMethod().

So by now, if we put together the things we learned, we can do something like this:

(function($){
   /**
     * default options for plugin
     * private
     * @var array defaultOptions
     */
    var defaultOptions = {
        action:       false,
        debug:        false
    }

    /**
     * yourPlugin base constructor
     *
     * @param [] options
     * @return void
     */
    $.fn.yourPlugin = function(options){
        // extend default options with overrides
        var opts = $.extend({}, defaultOptions, options);

        switch(opts.action)
        {
        	case 'private':
        		yourPrivateClassMethod();
        		break;

        	case 'public':
        		$.fn.yourPlugin.yourPublicClassMethod();
        		break;
        }
    }

    /**
     * public
     * method to do some vodoo
     */
    $.fn.yourPlugin.yourPublicClassMethod = function()
    {
    	// should alert 'false', 'false',
    	//because the public vars where not set

        alert($.fn.yourPlugin.defaultOptions.action);
        alert($.fn.yourPlugin.defaultOptions.debug);
    }

    /**
     * default options for plugin
     * public
     * @var array defaultOptions
     */
    $.fn.yourPlugin.defaultOptions = {
        action:       false,
        debug:        false
    }

    /**
     * private
     * method to do some vodoo
     */
    function yourPrivateClassMethod()
    {
        alert('This method is private');
    }

})(jQuery);

I hope this little summary helped a little in understanding how javascript classes and jQuery work.
If you have any suggestion or did see some errors in this tutorial please let me know about it.

1 Comment

  1. uberVU - social comments on November 24th, 2009

    Social comments and analytics for this post…

    This post was mentioned on Twitter by SwiftLizard: Wrote a little tutorial abou how to develop a jQuery Plugin http://www.swift-lizard.com/2009/11/15/how-to-develop-a-jquery-plugin/...



Leave a Reply

SwiftLizard Interactive {Design, Development}

  • Last Posts
    • Scrum Master
    • Loging into a page on shell with curl
    • Developing a clock in Flash with AS3
    • Javascript leashed with google Caja
    • Sometimes I wonder about Developers
  • Categories
    • css (1)
    • design (2)
    • development (2)
    • javascript (4)
    • jquery (3)
    • php (4)
    • project management (2)
    • scrum (2)
    • shell (1)
    • thoughts (7)
    • typo3 (3)
    • xhtml (1)
    • Zend Framework (1)
  • Tags
    actionscript 3.0 agile agile development caja continuous integration css design development Flash Flashbuilder javascript jquery Lean OpenAgile performance optimization php product design qunit scrum security shell sql thoughts typo3 xhtml zend zendFramework
  • Friends Blogs
    • Dmitry Dulepov Blog
    • Dominique Stender
    • Gina Steiners Blog
    • Iphone Fan by Dirk Kunde
    • Mario Rimann Blog
    • Michi Zepernick
    • Thomas Hempel :: Typo3 Unleashed
  • Archives
    • March 2010
    • December 2009
    • November 2009
  • Search


  • t3n Social News bookmark at mister wong publish in twitter bookmark at del.icio.us bookmark at digg.com bookmark at furl.net bookmark at linksilo.de bookmark at reddit.com bookmark at spurl.net bookmark at technorati.com bookmark at google.com bookmark at yahoo.com bookmark at facebook.com bookmark at stumbleupon.com bookmark at propeller.com bookmark at newsvine.com bookmark at jumptags.com

RSS Feed


  • Home
  • About
  • Links
  • Contact
  • Imprint

© Copyright SwiftLizard Interactive {Design, Development}. All rights reserved.
Designed by FTL Wordpress Themes brought to you by Smashing Magazine

Back to Top