Blog

Some of my recent posts and ramblings.

A simple jQuery plugin pattern

A simple jQuery plugin pattern. I had seen so many different patterns, Addy Osmani has a github repository full of them. This one works best for plugins which don’t have a lot of instances created for them. Like it’s not a good idea to use it for tooltips, etc (the basic jQuery plugin pattern is great for that) but its a great pattern for layout engines or slideshows which are usually called once or twice. I wrote this to avoid using so many ‘this’ and ‘that’ in the code.

The code layout is quite simple. An options and a state variable to keep track of the state and expose that and options, so they can be used for testing. We also expose out all the methods which we want to test or trigger manually in the return block. The ‘create’ method has all the initialization logic, all your event handlers go in the ‘setupEventHandlers’ method, the ‘destroy’ method implementation is optional, its useful to have it for testing which unbinds all the event handlers and clears timeouts.


Leave a Reply

Your email is never published nor shared. Required fields are marked *

You may use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>