Skip to content
This repository has been archived by the owner on Nov 10, 2017. It is now read-only.

Carry over fix for backbone issue 2724 #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
26 changes: 22 additions & 4 deletions backbone-events-standalone.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
var root = this,
breaker = {},
nativeForEach = Array.prototype.forEach,
nativeIsArray = Array.isArray,
hasOwnProperty = Object.prototype.hasOwnProperty,
slice = Array.prototype.slice,
toString = Object.prototype.toString,
idCounter = 0;

// Returns a partial implementation matching the minimal API subset required
Expand Down Expand Up @@ -68,6 +70,21 @@
func = null;
return memo;
};
},

isArray: nativeIsArray || function(obj) {
return toString.call(obj) == '[object Array]';
},

isString: function(obj) {
return toString.call(obj) == '[object String]';
},

isEmpty: function(obj) {
if (obj == null) return true;
if (_.isArray(obj) || _.isString(obj)) return obj.length === 0;
for (var key in obj) if (_.has(obj, key)) return false;
return true;
}
};
}
Expand Down Expand Up @@ -166,11 +183,12 @@
var listeners = this._listeners;
if (!listeners) return this;
var deleteListener = !name && !callback;
if (typeof name === 'object') callback = this;
if (!callback && typeof name === 'object') callback = this;
if (obj) (listeners = {})[obj._listenerId] = obj;
for (var id in listeners) {
listeners[id].off(name, callback, this);
if (deleteListener) delete this._listeners[id];
obj = listeners[id];
obj.off(name, callback, this);
if (deleteListener || _.isEmpty(obj._events)) delete this._listeners[id];
}
return this;
}
Expand Down Expand Up @@ -230,7 +248,7 @@
var listeners = this._listeners || (this._listeners = {});
var id = obj._listenerId || (obj._listenerId = _.uniqueId('l'));
listeners[id] = obj;
if (typeof name === 'object') callback = this;
if (!callback && typeof name === 'object') callback = this;
obj[implementation](name, callback, this);
return this;
};
Expand Down
4 changes: 2 additions & 2 deletions backbone-events-standalone.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,20 @@ describe("BackboneEvents", function() {
expect(true).ok;
});

it("stopListening cleans up references", function() {
var a = _.extend({}, BackboneEvents);
var b = _.extend({}, BackboneEvents);
var fn = function() {};
a.listenTo(b, 'all', fn).stopListening();
expect(_.size(a._listeners)).eql(0);
a.listenTo(b, 'all', fn).stopListening(b);
expect(_.size(a._listeners)).eql(0);
a.listenTo(b, 'all', fn).stopListening(null, 'all');
expect(_.size(a._listeners)).eql(0);
a.listenTo(b, 'all', fn).stopListening(null, null, fn);
expect(_.size(a._listeners)).eql(0);
});

it("trigger all for each event", function() {
var a, b, obj = { counter: 0 };
BackboneEvents.mixin(obj);
Expand Down