Which plugin design pattern should I be using? And how?

Which plugin design pattern should I be using? And how?


I'm writing - well, have written - a plugin for adding pagination elements to a page. However, I'm confused about what structure the plugin should take. The basic problem, I guess, is that the plugin doesn't really target one element or group of elements. It adds HTML to various bits all over the page. And I've now found myself doing this:

  1. (function($) {
  2.       $.fn.paginate = function(params) {

  3.             var defaults = {
  4.                   // defaults here
  5.             }
  6.             var options = $.extend(defaults, params);

  7.             callAFunction();
  8.             callAnotherFunction();
  9.             return this.each(function(i, element) {
  10.                   // nothing to do here, I'm doing all the heavy lifting elsewhere
  11.             });
  14.       };
  15. })(jQuery);

I'm calling the plugin on the divs that will contain the numbered page links:

  1. $('.pagination_links').paginate(paginate_params);

But there doesn't seem to be any point to doing that: the pagination links containers aren't any more 'special' than any of the other elements that the plugin has to affect.

What are you supposed to do instead, though? Attach the plugin to jQuery rather than the fn object? It seems a bit... presumptuous to be adding my plugin to the jQuery core.

But if that is the right approach, what's the structure of such a plugin? Where do your plugins' functions go, how are they called and what are you suppoed to return?

Thanks :)