JQuery – Do you really need that plugin?
I found this post on DZone about an form autosave plugin for JQuery, i visited the plugin page, read about everything that is capable to do, and concluded that it could be a really handy and useful plugin. But it made me think… Do I really need that every time I want an auto saving form? This post is not about this plugin in particular, but about many plugins, and it can be applied to other frameworks, javascript or not.
I write lots of fancy rich internet applications, and I use JQuery a lot. Sometimes I look at all the plugins I’m using for a particular project and I think to myself, “man, those are lots of plugins to use…”. I’m pretty sure this auto save plugin has many great usages and there are some scenarios that make it indispensable, but for the majority of cases we could just simply write a bunch of lines of code to achieve what we’re looking for. Out of the top of my head here’s a very simple (not tested) snippet that auto saves a form every time a form element changes:
$(function() { var $form = $('#myform'); var $fields = $form.find('input, select, textarea'); $fields.change(function() { var formdata = { 'isdraft': 'yes' }; $fields.each(function() { formdata[$(this).attr('name')] = $(this).val(); }); $.post($form.attr('action'), formdata); }); });
According to the plugin’s page, you can achieve the same with just one line of code, what is certainly nice, but let’s consider the drawbacks. The plugin in its packed version occupies 2.7k (5.1k unpacked), and it depends on another plugin to work, which adds an extra 1 or 2k at least. Then there is the page loading time, the javascript parser has to read the entire plugin, even if we just use the most basic functionality. If we’re just using this plugin, that is not a big problem, but if we already have other ten plugins.
Finally, don’t forget that nobody knows your scenario better than you do. The plugin authors try to make them usable on many possible situations, and are not considering your particular case. Plugins are a great help in accomplishing complex tasks or when the behavior of your code depends on external variables, like user preferences, but if your task is simple enough to be written in just a bunch of code lines, you probably don’t need a plugin…