Hi All,
I'm trying to figure out the best way to organize my jQuery code. I have a simple case that I would like to discuss here to improve my, and hopefully many others', code patterns.
The case
Build a flexible
'slider (as in Slideshow)' plugin/utility, that can take multiple sliders (each having one slide visible at a time) and multiple controls (arrows, bullets). A simplified html example would be:
- <ul class='slider' id='main-slider'>
- <li>Slide 1</li><li>Slide 2</li><li>Slide 3</li>
- </ul>
- <ul class='slider' id='slider-captions'>
- <li>Caption 1</li><li>Caption 2</li><li>Caption 3</li>
- </ul>
- <div class='control-prev'>Previous</div>
- <div class='control-next'>Next</div>
- <ul class='bullets'>
- <li></li><li></li><li></li>
- </ul>
The functionality required:
- Both sliders start at index 0 (showing 'Slide1' and 'Caption 1')
- When clicked 'previous' the previous slide is shown in both sliders (in this case 3)
- When clicked 'next' the next slide is shown in both sliders (in this case 2)
- When clicked on one of the bullets, depending on its index, the corresponding slide is shown in both sliders.
- Also, when a slide changes, the bullets need to be updated (so the corresponding bullet is highlighted)
- The markup can be anywhere in the document, so it can not be put in one container.
I would like this discussion
not to be about the implementation, but only about the interface. In other words, I would like to discuss how (when this plugin is installed) you go about attaching the functionality to the DOM elements.
Consider the following three alternatives:
Option 1 - No plugin
I'm using pseudo-code to keep things clear..
- $('.control-prev').click(function(){
- // check which slide is shown
- // if the first slide, jump to the last slide (if looping is allowed)
- // show the correct slide in both sliders
- // highlight the correct bullet in the bullet navigation
- // disable this control if looping is not allowed (e.g. hide the control)
- });
- $('.control-next').click( /* same here */ );
- $('.bullets li').click( function(){
- // check the index of the clicked bullet
- // hide the current slide
- // show the correct slide
- // highlight the correct bullet
- // disable the 'previous' or 'next' control if the first or last slide (resp.) is shown, and looping is not allowed
- });
Now imagine we have some button on the page to make slide 2 show up. We would have to check if the slide exists, show the slide in both sliders, highlight the correct bullet and possibly disable some 'previous' or 'next' controls.
Isn't there a simpler way?
Option 2 - $(element).sliderPlugin()
Again, without going into detailed implementation, this is what it would look like when you assign the plugin(s) to your elements.
- $('.slider').slider({allowLooping:false}); // This applies to both sliders.
- $('.control-next').slideControl({slider:$('.slider'), action:'next'});
- $('.control-prev').slideControl({slider:$('.slider'), action:'prev'});
- $('.bullets').slideNavigation({slider:$('.slider')});
That looks a lot better, don't you think? Though the functionality may be the same as option 1, we don't have to clutter our $(document).ready function with all that code.
But this still requires 3 different plugins, and raises some flexibility questions should we want to change something about our slider. How can I manually call for the 'next' slide? Which slider is then in charge of updating the controls?
Option 3 - $.makeSlider() Utility
Imagine you could install a plugin and write the following code to build your slider:
- var slider = $.makeSlider({loop:false})
- .addSlider($('.slider')) // this adds all sliders with class .slider
- .addControl($('.control-prev'), 'prev')
- .addControl($('.control-next'), 'next')
- .addControl($('.bullets'), 'index')
- // Whenever you want, you can call:
- slider.next()
- slider.goTo(2)
- // Or adjust your slider's configuration:
- .addSlider($('.someotherslider'))
- .removeControl($('.control-prev'))
- .allowLoop(true)
Now instead of method chaining per element, the methods are chained on the controlling object (which you get from the utility $.makeSlider()).
Comparison
It shouldn't be a surprise to you that I really prefer the third option. But because I haven't seen this pattern in many places I'm looking for some confirmation or critique from you guys.
Please let me know what you think of the third option and why it should be used or not.