Skip to content

Commit

Permalink
Unbind on destroy; improve unbind logic
Browse files Browse the repository at this point in the history
  • Loading branch information
megawac committed Jan 20, 2016
1 parent 667a6bc commit eab42e1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
15 changes: 7 additions & 8 deletions lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,14 @@ Backbone.Collection.prototype.ioUnbind = function (eventName, io, callback) {
}
var events = ioEvents[eventName];
if (!_.isEmpty(events)) {
if (callback && 'function' === typeof callback) {
for (var i = 0, l = events.length; i < l; i++) {
if (callback == events[i].cbLocal) {
this.unbind(events[i].name, events[i].cbLocal);
io.removeListener(events[i].global, events[i].cbGlobal);
events[i] = false;
if ('function' === typeof callback) {
_.each(events, function(event, index) {
if (callback == event.cbLocal) {
this.unbind(event.name, event.cbLocal);
io.removeListener(event.global, event.cbGlobal);
events.splice(index, 1);
}
}
events = _.compact(events);
});
} else {
this.unbind(eventName);
io.removeAllListeners(globalName);
Expand Down
16 changes: 8 additions & 8 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Backbone.Model.prototype.ioBind = function (eventName, io, callback, context) {
};
this.bind(event.name, event.cbLocal, (context || self));
io.on(event.global, event.cbGlobal);
this.once('destroy', _.bind(this.ioUnbind, this, event.name, io, event.cbLocal));
if (!ioEvents[event.name]) {
ioEvents[event.name] = [event];
} else {
Expand Down Expand Up @@ -97,15 +98,14 @@ Backbone.Model.prototype.ioUnbind = function (eventName, io, callback) {
}
var events = ioEvents[eventName];
if (!_.isEmpty(events)) {
if (callback && 'function' === typeof callback) {
for (var i = 0, l = events.length; i < l; i++) {
if (callback == events[i].cbLocal) {
this.unbind(events[i].name, events[i].cbLocal);
io.removeListener(events[i].global, events[i].cbGlobal);
events[i] = false;
if ('function' === typeof callback) {
_.each(events, function(event, index) {
if (callback == event.cbLocal) {
this.unbind(event.name, event.cbLocal);
io.removeListener(event.global, event.cbGlobal);
events.splice(index, 1);
}
}
events = _.compact(events);
});
} else {
this.unbind(eventName);
io.removeAllListeners(globalName);
Expand Down

0 comments on commit eab42e1

Please sign in to comment.