I hope the question is not too unspecific. At the moment, I am trying to write a plugin for jQuery, but I struggle with the various ways to structure it. I found bxSlider, and thought this was pretty accessible for plugin code. Here is a simple example:
(function($){
$.fn.letSlider = function(options){
var defaults = {
speed: 500,
onBeforeSlide: function(){} //Callback
}
var options = $.extend(defaults, options);
//cache base element
base = this;
//public functions
this.init = function(){
text = giveText();
alert(text + ' ' + options.speed);
};
//private functions
function giveText(){
return 'blubb';
};
this.each(function(){
// make sure the element has children
if($(this).children().length > 0){
base.init();
}
});
return this;
}
})(jQuery);
So it has a clear structur which I manage to understand. However, it looks quite different from all plugin structures I have seen advised so far, so I am not sure, if it won't cause me trouble later on. Still, it does basically everything I need: exposed options, callbacks, private and public functions. And with this.each it should be chainable too. One thing I noticed already is that basically everything is in the initial $.fn.bxSlider = function(options){ ... and I have never seen that before.
As far as the other plugin structures go, they are either too complicated for me to grasp (shame on me), or I fail at implementing them. For example, with http://www.learningjquery.com/2007/10/a-plugin-development-pattern I failed to make the opts (options variable) available in private or public functions. But I digress. ...