One Plugin to end all jQuery Plugins

Intro

Contents

Meta Plugin
Meta Plugin

Well, I could not resist this catchy title, could I? Perhaps I could. In any case I have this idea and implementation where one jQuery plugin is sort-of-a “socket” (not a network socket, but a socket in the wall) into which you can plug-in your “plugs” which will do standard jQuery related stuff.

Where “standard jQuery related stuff” is either changing the state of the DOM behind, or either using the information from the DOM to do something with it.

Example of the former is much loved “animation” stuff one can do with jQuery. Example of the latter, could be collecting some information from the DOM entities selected with the jQuery and doing something with that information. Sending it to some server or just displaying it would qualify as “doing something with the information”.

As it turns out it is easier to use jQuery for changing the state of the dom, then it is to use information that it can provide from the DOM, of the current page. This is because the latter involves your code which has nothing to do with the stuff that jQuery does. Strictly speaking this is not “difficult” , it is just not “naturally flowing” as jQuery chains of commands do.

Perhaps this looks a bit trivial, and you are perhaps wondering what am I going on about here, and why are you reading this post? You are demanding it,  and now I have to find an convincing use-case , to er… convince you. Ok, here we go. Consider writing a code that will “blanket” any user selected number of elements on the page, by placing one div “over” it. This div has to be just big enough to cover all the elements selected. We can make it transparent and/or write some text on it etc … Convincing enough for You? Good. So we have this clever algorithm which given set of overlapping squares, returns one square position and dimensions that “blankets” them all. Obviously we can imagine an plugin that does all of this. Walks through the current “stack” of selected elements, collects positions and dimensions of each , etc … And that is the point: plugin is required.

Each time you want to take something “out” form the jQuery object it is best to write a plugin that will take that “thing out” and then return it, or do something with it, and then return the jQuery instance (doing: return this;) so that Your beloved chaining is not broken.

If we could do the above we could have one (very) nice framework that would allow us to re use the plugin above for virtually “anything”. The actual solution will be in the callback given to the plugin. For example we could try solve the “blanketing” ?
Lovely … But. Will this work ? For starters, call to the callback() above may block the plugin or throw an exception. Fine. We can solve that.
Renamed it to “use”. This will execute the callback on a different call-path (not thread), and will also handle the exception thrown , if any. error_handler is either user defined or console or alert.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.