jquery plugin with multiple functions

Posted by tcurdt on Stack Overflow See other posts from Stack Overflow or by tcurdt
Published on 2010-04-12T18:43:44Z Indexed on 2010/04/12 18:53 UTC
Read the original article Hit count: 339

Filed under:
|
|

According to the developer documentation jquery plugins are supposed to have only one namespace for all functions they make available. Which is straight forward as long as you only expose a single function per context (static/element).

(function($){

    var

    state_a = 0,

    $.myplugin = function(in_options) {
      // static
      return this;
    }

    $.fn.myplugin = function(in_options) {
      // element
      return this;
    }

})(jQuery);

This makes calls like this possible:

$("elem").myplugin(options);
jQuery.myplugin(options);

What's the best approach if you have more than one function and need to share state? I would like to call into my plugin like this:

$("elem").myplugin.start(options);
$("elem").myplugin.stop();
jQuery.myplugin.start(options);
jQuery.myplugin.stop();

© Stack Overflow or respective owner

Related posts about jquery-plugins

Related posts about jQuery