So I have quite a few jQuery plugins in my arsenal now. Right now all of the plugin names are prefixed with ‘sv_’, for example ‘sv_tooltip’, to reduce name collisions with other plugins. I was trying to think of other ways to namespace my plugins. After some googling and playing around I came across 3 ways to namespace them so far.
Which would leave me with one of these syntax’s when initializing the plugins.
1.
$('selector').namespace('pluginName', options);
2.
$('selector').namespace().pluginName(options);
3.
$.namespace('selector').pluginName(options);
My question is have any of you namespaced a collection of your plugins, and if so what technique do you use? OR do you know of any plugins that use a different namespace technique then what I listed above.
- United States
- Has been a member for 4-5 years
- Exclusive Author
- Author was Featured
- Sold between 50 000 and 100 000 dollars
- Item was Featured
- Contributed a Tutorial to a Tuts+ Site
- Author had a Free File of the Month
I’m not sure adding a namespace in any of those ways is any different than the “sv_” prefix. Because couldn’t you have a collision with the namespace name as well?
It is no different, I am just interested in looking at other options. No real reason as to why, just something that caught my interest for a minute.
So a little playing around and if I did this the plugins could be called in any of the ways I mentioned above…
(function ($) {
$.sv = $.sv || function(obj){ return $(obj).sv(); };
$.sv.fn = $.sv.fn || {};
if(!$.fn.sv){
$.fn.sv = function( method ){
if ( method && $.sv.fn[method] ) {
return $.sv.fn[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
}
else {
$.extend(this, $.sv.fn);
return this;
}
}
}
})(jQuery);
I would just have to define my plugins like this instead…
$.sv.fn.tooltip = function( options ) {
}
A bit useless right now, but fun playing with..
