ActiveDen

jQuery plugin namespacing, what do you do?

443 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Sold between 1 000 and 5 000 dollars
  • United States
SplitV says

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.

5008 posts The Dude Abides
  • United States
  • Elite Author
  • Has been a member for 4-5 years
  • Exclusive Author
  • Sold between 50 000 and 100 000 dollars
  • Contributed a Tutorial to a Tuts+ Site
  • Author had a Free File of the Month
+4 more
CodingJack says

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?

443 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Sold between 1 000 and 5 000 dollars
  • United States
SplitV says

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.

443 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Sold between 1 000 and 5 000 dollars
  • United States
SplitV says

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..

by
by
by
by
by