Which plugin design pattern should I be using? And how?
Hi,
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:
- (function($) {
- $.fn.paginate = function(params) {
- var defaults = {
- // defaults here
- }
- var options = $.extend(defaults, params);
- callAFunction();
- callAnotherFunction();
- return this.each(function(i, element) {
- // nothing to do here, I'm doing all the heavy lifting elsewhere
- });
-
-
- };
- })(jQuery);
I'm calling the plugin on the divs that will contain the numbered page links:
- $('.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 :)