blob: 5d24a7efc3ad756a94e8396f75077df476f0da96 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
/*
* jQuery Condom (Use namespaces to protect your global integrity.)
* Version 0.0.3
*
* Copyright (c) 2011 Mario "Kuroir" Ricalde (http://kuroir.com)
* & Micha Niskin (micha@thinkminimo.com)
* Licensed jointly under the GPL and MIT licenses,
* choose which one suits your project best!
*/
(function($) {
var methods = {};
$.ns = function(ns) {
// Define namespace if it doesn't exist.
methods[ns] = methods[ns] || {};
// Get reference to a namespaced jQ object
function nsfun(selector, context) {
return $(selector, context).ns(ns);
}
// Allows you to add methods ala jQuery.fn (useful to namespace premade plugins)
nsfun.fn = methods[ns];
// Add a method.
nsfun.add = function(fname, fn) {
var new_funcs = typeof fname == "object" ? fname : {};
// One method.
if (new_funcs !== fname)
new_funcs[fname] = fn;
// Group of methods.
$.each(new_funcs, function(fname, fn) {
methods[ns][fname] = function() {
fn.apply(this, arguments);
return this;
};
});
return this;
};
// Get methods.
nsfun.methods = function() {
return $.extend({}, methods[ns]);
};
return nsfun;
};
// The only function that touches $.fn
$.fn.ns = function(ns) {
if (methods[ns]) $.extend(this, methods[ns]);
return this;
};
})(jQuery);
|