Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

off() can now be used with events, functions, contexts or **any combi…

…nation thereof**
  • Loading branch information...
commit eb275fac1fa7193b5f7b8e519bc7a1c2610171c5 1 parent f4e952b
@malandrew authored
Showing with 28 additions and 8 deletions.
  1. +28 −8 backbone.js
View
36 backbone.js
@@ -104,23 +104,43 @@
// Remove one or many callbacks. If `context` is null, removes all callbacks
// with that function. If `callback` is null, removes all callbacks for the
// event. If `ev` is null, removes all bound callbacks for all events.
- off: function(events, callback, context) {
- var ev, calls, node;
- if (!events) {
- delete this._callbacks;
+ off: function() {
+ var ev, calls, node, events, callback, context;
+ if (arguments.length === 0) {
+ delete this._callbacks
} else if (calls = this._callbacks) {
- events = events.split(/\s+/);
+ for (i = 0, length = arguments.length; i < length; i++) {
@malandrew Owner

Fix i leaking into global namespace

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ switch (typeof arguments[i]) {
+ case 'string':
+ events = arguments[i].split(/\s+/); break;
+ case 'function':
+ callback = arguments[i]; break;
+ case 'object':
+ context = arguments[i]; break;
+ }
+ }
+ if (!events) {
+ events = [];
+ for (ev in calls) {
+ events.push(ev)
+ }
+ }
while (ev = events.shift()) {
node = calls[ev];
delete calls[ev];
- if (!callback || !node) continue;
+ if (!node) continue;
+ //if (!callback || !node) continue;
@malandrew Owner

note to self: remove this comment before submitting pull request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
// Create a new list, omitting the indicated event/context pairs.
while ((node = node.next) && node.next) {
- if (node.callback === callback &&
+ if (ev === 'an_event') {
+ if ((!callback || node.callback === callback) &&
+ (!context || node.context === context)) console.log("off");
+ }
+ if ((!callback || node.callback === callback) &&
(!context || node.context === context)) continue;
this.on(ev, node.callback, node.context);
}
- }
+ }
}
return this;
},
@malandrew

note to self: remove this comment before submitting pull request

@malandrew

Fix i leaking into global namespace

Please sign in to comment.
Something went wrong with that request. Please try again.