Skip to content
This repository was archived by the owner on Dec 4, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ $.ns('myNamespace').add({
});
```

### jQuery.ns().patch
Monkey-patches the given method(s), prepending the original, unpatched jQuery method to the arguments passed to the method when called.

#### Example
```javascript
$.ns('myNamespace').patch({
toggle: function(orig) {
console.log("this is the patched toggle method");
// Call the unpatched method now, with the arguments the patched method
// was called with.
return orig.apply(this, $.makeArray(arguments).slice(1));
}
});
```

### jQuery.ns().methods
Return the available methods to the given namespace, this can be used to check if the namespace is defined previously. Useful when distributing your libraries that have common namespace names.
Expand Down
33 changes: 23 additions & 10 deletions jquery.condom.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,31 @@
// Allows you to add methods ala jQuery.fn (useful to namespace premade plugins)
nsfun.fn = methods[ns];

// If `key` is an object val is ignored and `key` is returned. Otherwise,
// an object is returned with obj[key] set to `val`.
function asObj(key, val) {
var obj = $.type(key) == "object" ? key : {};
if (obj !== key)
obj[key] = val;
return obj;
}

// 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;
};
$.each(asObj(fname, fn), function(fname, fn) {
methods[ns][fname] = function() { return fn.apply(this, arguments) };
});
return this;
};

// Monkey-patch a jQuery instance method.
nsfun.patch = function(fname, fn) {
$.each(asObj(fname, fn), function(fname, fn) {
methods[ns][fname] = (function(orig) {
return function() {
return fn.apply(this, [orig].concat($.makeArray(arguments)));
};
})($.fn[fname]);
});
return this;
};
Expand Down