So in the first part we talked generically about some design decisions surrounding making jQuery plugins. Now, I want to focus specifically on my Column View jQuery plugin and some of the design decisions I needed to make when writing it.
I really knew I only had one requirement beyond reproducing the metaphor from OS X’s Finder. I knew it had to be dynamic. I couldn’t just ask whoever wants to use this to display all of their column data onto the page and link them all together. It would create a nightmare to maintain or even develop in certain situations. So, that meant I needed to create DOM elements.
But, I knew the user needed to be in control of the DOM. They need to style, place, and size the entire column view widget. Some plugins (e.g. Fancybox, which is fantastic) take the other route by doing all the DOM work for you. All you do is provide the content. For that kind of plugin the design decision is perfect – it is a lightbox, it needs to a do a lot of DOM work and styling to accomplish the task.
So I decided to only manipulate the DOM once during the setup of the plugin; I take the contents of the node and place that in the first column:
1 | div.html(columnview.contents().detach()).appendTo(columnview); //replace contents into new column |
This way any developer can style the node they designate as the column view widget and I leave that entirely alone. They can be sure that their work remains intact and functional.
To keep the metaphor going and keep the widget dynamic, I also needed to add a way to reload a specific column. When you make a new file or folder in the OS X Finder you can see it right away in the column. To accomplish this, I needed to store the loaded url of each column. As I described last time, the best way to store data is with jQuery’s `data` method, so whenever a column’s contents are updated I place the called url onto it’s DOM node with a `data` call.
I am sure there are lots of different ways to accomplish this task. By defining my one operating principle (being dynamic) all my execution questions could easily be answered. Overall I got a very minimalist approach that would not get in a developer’s way. Some might label this as “unpolished”. But I disagree. Write as little code as you need to the job done, right? I’d like to hear if you agree or disagree? Or perhaps, what else you think this widget needs to do, or could do?



