JQuery plugin: creating a function in '$'
I created a small jQuery scroll plugin. It has a fairly standard implementation: $(selector).drollScroll({options}); , complete with default values. The trick is that I would like the user to be able to set these defaults in advance, so that for larger sites (I design this for a larger site), future users can easily get the same scrollbars without specifying parameters each time.
I would really like the user to do this without the first instance of the jQuery object, so through something like $.drollScrollDefaults({options}) .
So far I have this:
(reduced to the level necessary for reading pleasure)
(function($) { var defaults = { content : ".drollScrollBowl", scrollBoxClassName : "drollScrollShoal", scrollbarClassName : "drollScrollPole", thumbClassName : "drollScrollTrollThumb", scrollbarOpacity : 1, scrollbarFadeTime : 300, scrollbarTimeToLive : 1000, alwaysVisible : true, autoFade : false, overrideBoxStyling : false, overrideBarStyling : false, overrideThumbStyling : false, scrollWidthConstant : false }; $.drollScrollDefaults = function(options) { $.extend(defaults, options); }; $.fn.drollScroll = function(options) { var opts; return this.each(function() { // we want opts to be its own object in case someone edits it later opts = $.extend({}, defaults, options); $(this).data("drollScroll", {options : opts}); }); }; })(jQuery); Now it works, but this line
$.drollScrollDefaults = function(options) seriously cannot be how it should be done. It feels hacked; I feel like I should be doing it wrong, as if I should use the (totally mystical for me) $ prototype or something else. It? Am I right already? What is the best way to achieve this? Should I do this ??
I usually define it for a function, for example,
$.fn.myplugin = function() { ... your plugin here ... }; $.fn.myplugin.defaults = { ... defaults here ...}; primarily because most of my plugins can be called $.myplugin and $().myplugin()
Access it inside your plugin and outside using $.fn.myplugin.defaults