Skip to content

Commit ecdaed6

Browse files
committed
Use the public on method when listening
This uses a private `listening` var to share state between a Backbone "listener" and "listenee", instead of using a private `internalOn()` to share state. This allows `#listenTo` to use the public `#on` method and keeps interop between Backbone and any other event library.
1 parent f430fa0 commit ecdaed6

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

backbone.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@
8787
// Regular expression used to split event strings.
8888
var eventSplitter = /\s+/;
8989

90+
// A private global variable to share between listeners and listenees.
91+
var listening;
92+
9093
// Iterates over the standard `event, callback` (as well as the fancy multiple
9194
// space-separated events `"change blur", callback` and jQuery-style event
9295
// maps `{event: callback}`), reducing them by manipulating `memo`.
@@ -113,24 +116,18 @@
113116
// Bind an event to a `callback` function. Passing `"all"` will bind
114117
// the callback to all events fired.
115118
Events.on = function(name, callback, context) {
116-
return internalOn(this, name, callback, context);
117-
};
118-
119-
// An internal use `on` function, used to guard the `listening` argument from
120-
// the public API.
121-
var internalOn = function(obj, name, callback, context, listening) {
122-
obj._events = eventsApi(onApi, obj._events || {}, name, callback, {
123-
context: context,
124-
ctx: obj,
125-
listening: listening
119+
this._events = eventsApi(onApi, this._events || {}, name, callback, {
120+
context: context,
121+
ctx: this,
122+
listening: listening
126123
});
127124

128125
if (listening) {
129-
var listeners = obj._listeners || (obj._listeners = {});
126+
var listeners = this._listeners || (this._listeners = {});
130127
listeners[listening.id] = listening;
131128
}
132129

133-
return obj;
130+
return this;
134131
};
135132

136133
// Inversion-of-control versions of `on`. Tell *this* object to listen to
@@ -139,7 +136,7 @@
139136
if (!obj) return this;
140137
var id = obj._listenId || (obj._listenId = _.uniqueId('l'));
141138
var listeningTo = this._listeningTo || (this._listeningTo = {});
142-
var listening = listeningTo[id];
139+
listening = listeningTo[id];
143140

144141
// This object is not listening to any other events on `obj` yet.
145142
// Setup the necessary references to track the listening callbacks.
@@ -149,10 +146,20 @@
149146
}
150147

151148
// Bind callbacks on obj, and keep track of them on listening.
152-
internalOn(obj, name, callback, this, listening);
149+
var error = tryCatchOn(obj, name, callback, this);
150+
listening = void 0;
151+
if (error) throw error;
153152
return this;
154153
};
155154

155+
var tryCatchOn = function(obj, name, callback, context) {
156+
try {
157+
obj.on(name, callback, context);
158+
} catch (e) {
159+
return e;
160+
}
161+
};
162+
156163
// The reducing API that adds a callback to the `events` object.
157164
var onApi = function(events, name, callback, options) {
158165
if (callback) {
@@ -172,8 +179,8 @@
172179
Events.off = function(name, callback, context) {
173180
if (!this._events) return this;
174181
this._events = eventsApi(offApi, this._events, name, callback, {
175-
context: context,
176-
listeners: this._listeners
182+
context: context,
183+
listeners: this._listeners
177184
});
178185
return this;
179186
};

0 commit comments

Comments
 (0)