javascript plugin - singleton pattern?

Posted by Adam Kiss on Stack Overflow See other posts from Stack Overflow or by Adam Kiss
Published on 2010-03-08T21:41:01Z Indexed on 2010/03/08 21:51 UTC
Read the original article Hit count: 335

Filed under:
|
|
|

intro

Hello,

I'm trying to develop some plugin or/*and* object in javascript, which will control some properties over some object. It will also use jQuery, to ease development.

idea

This is in pseudocode to give you an idea, since I can't really describe it in english with the right words, it's impossible to go and use google to find out what I want to use (and learn).

I will have plugin (maybe object?) with some variables and methods:

  plugin hideshow(startupconfig){
    var c, //collection
    add: function(what){
      c += what;
    },
    do: function(){
      c.show().hide().stop(); //example code
    }
  }

and I will use it this way (or sort-of):

  var p = new Plugin();

  p
   .add($('p#simple'))
   .add($('p#simple2'))
   .do();

note

I'm not really looking for jQuery plugin tutorials - it's more like usage of singleton pattern in document in javascript, jQuery is mentione only because it will be used to modify dom and simplify selectors, jQuery plugin maybe just for that one little function add.

I'm looking for something, that will sit on top of my document and call functions from itselft based on timer, or user actions, or whatever.

problems

  1. I don't really know where to start with construction of this object/plugin
  2. I'm not really sure how to maintain one variable, which is collection of jQuery objects (something like $('#simple, #simple2');)
  3. I would maybe like to extedn jQuery with $.fn.addToPlugin to use chaining of objects, but that should be simple (really just each( p.add($(this)); ))

I hope I make any sense, ideas, links or advices appreciated.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about plugins