|
87 | 87 | // Regular expression used to split event strings. |
88 | 88 | var eventSplitter = /\s+/; |
89 | 89 |
|
| 90 | + // A private global variable to share between listeners and listenees. |
| 91 | + var listening; |
| 92 | + |
90 | 93 | // Iterates over the standard `event, callback` (as well as the fancy multiple |
91 | 94 | // space-separated events `"change blur", callback` and jQuery-style event |
92 | 95 | // maps `{event: callback}`), reducing them by manipulating `memo`. |
|
113 | 116 | // Bind an event to a `callback` function. Passing `"all"` will bind |
114 | 117 | // the callback to all events fired. |
115 | 118 | 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 |
126 | 123 | }); |
127 | 124 |
|
128 | 125 | if (listening) { |
129 | | - var listeners = obj._listeners || (obj._listeners = {}); |
| 126 | + var listeners = this._listeners || (this._listeners = {}); |
130 | 127 | listeners[listening.id] = listening; |
131 | 128 | } |
132 | 129 |
|
133 | | - return obj; |
| 130 | + return this; |
134 | 131 | }; |
135 | 132 |
|
136 | 133 | // Inversion-of-control versions of `on`. Tell *this* object to listen to |
|
139 | 136 | if (!obj) return this; |
140 | 137 | var id = obj._listenId || (obj._listenId = _.uniqueId('l')); |
141 | 138 | var listeningTo = this._listeningTo || (this._listeningTo = {}); |
142 | | - var listening = listeningTo[id]; |
| 139 | + listening = listeningTo[id]; |
143 | 140 |
|
144 | 141 | // This object is not listening to any other events on `obj` yet. |
145 | 142 | // Setup the necessary references to track the listening callbacks. |
|
149 | 146 | } |
150 | 147 |
|
151 | 148 | // 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; |
153 | 152 | return this; |
154 | 153 | }; |
155 | 154 |
|
| 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 | + |
156 | 163 | // The reducing API that adds a callback to the `events` object. |
157 | 164 | var onApi = function(events, name, callback, options) { |
158 | 165 | if (callback) { |
|
172 | 179 | Events.off = function(name, callback, context) { |
173 | 180 | if (!this._events) return this; |
174 | 181 | this._events = eventsApi(offApi, this._events, name, callback, { |
175 | | - context: context, |
176 | | - listeners: this._listeners |
| 182 | + context: context, |
| 183 | + listeners: this._listeners |
177 | 184 | }); |
178 | 185 | return this; |
179 | 186 | }; |
|
0 commit comments