|
1 | | -// Backbone.Radio v0.8.5 |
| 1 | +// Backbone.Radio v0.9.0 |
2 | 2 | (function(root, factory) { |
3 | 3 | if (typeof define === 'function' && define.amd) { |
4 | 4 | define(['backbone', 'underscore'], function(Backbone, _) { |
|
20 | 20 |
|
21 | 21 | var Radio = Backbone.Radio = {}; |
22 | 22 |
|
23 | | - Radio.VERSION = '0.8.5'; |
| 23 | + Radio.VERSION = '0.9.0'; |
24 | 24 |
|
25 | 25 | // This allows you to run multiple instances of Radio on the same |
26 | 26 | // webapp. After loading the new version, call `noConflict()` to |
|
35 | 35 | // get around the issues of lack of warnings when events are mis-typed. |
36 | 36 | Radio.DEBUG = false; |
37 | 37 |
|
| 38 | + // Format debug text. |
| 39 | + Radio._debugText = function(warning, eventName, channelName) { |
| 40 | + return warning + (channelName ? ' on the ' + channelName + ' channel' : '') + |
| 41 | + ': "' + eventName + '"'; |
| 42 | + }; |
| 43 | + |
38 | 44 | // This is the method that's called when an unregistered event was called. |
39 | 45 | // By default, it logs warning to the console. By overriding this you could |
40 | 46 | // make it throw an Error, for instance. This would make firing a nonexistent event |
41 | 47 | // have the same consequence as firing a nonexistent method on an Object. |
42 | | - function debugLog(warning, eventName, channelName) { |
43 | | - if (!Radio.DEBUG) { return; } |
44 | | - var channelText = channelName ? ' on the ' + channelName + ' channel' : ''; |
45 | | - if (console && console.warn) { |
46 | | - console.warn(warning + channelText + ': "' + eventName + '"'); |
| 48 | + Radio.debugLog = function(warning, eventName, channelName) { |
| 49 | + if (Radio.DEBUG && console && console.warn) { |
| 50 | + console.warn(Radio._debugText(warning, eventName, channelName)); |
47 | 51 | } |
48 | | - } |
| 52 | + }; |
49 | 53 |
|
50 | 54 | var eventSplitter = /\s+/; |
51 | 55 |
|
52 | 56 | // An internal method used to handle Radio's method overloading for Requests and |
53 | 57 | // Commands. It's borrowed from Backbone.Events. It differs from Backbone's overload |
54 | 58 | // API (which is used in Backbone.Events) in that it doesn't support space-separated |
55 | 59 | // event names. |
56 | | - function eventsApi(obj, action, name, rest) { |
| 60 | + Radio._eventsApi = function(obj, action, name, rest) { |
57 | 61 | if (!name) { |
58 | 62 | return false; |
59 | 63 | } |
60 | 64 |
|
61 | | - var results = []; |
| 65 | + var results = {}; |
62 | 66 |
|
63 | 67 | // Handle event maps. |
64 | 68 | if (typeof name === 'object') { |
65 | 69 | for (var key in name) { |
66 | 70 | var result = obj[action].apply(obj, [key, name[key]].concat(rest)); |
67 | | - eventSplitter.test(key) ? (results = results.concat(result)) : results.push(result); |
| 71 | + eventSplitter.test(key) ? _.extend(results, result) : results[key] = result; |
68 | 72 | } |
69 | 73 | return results; |
70 | 74 | } |
|
73 | 77 | if (eventSplitter.test(name)) { |
74 | 78 | var names = name.split(eventSplitter); |
75 | 79 | for (var i = 0, l = names.length; i < l; i++) { |
76 | | - results.push(obj[action].apply(obj, [names[i]].concat(rest))); |
| 80 | + results[names[i]] = obj[action].apply(obj, [names[i]].concat(rest)); |
77 | 81 | } |
78 | 82 | return results; |
79 | 83 | } |
80 | 84 |
|
81 | 85 | return false; |
82 | | - } |
| 86 | + }; |
83 | 87 |
|
84 | 88 | // An optimized way to execute callbacks. |
85 | | - function callHandler(callback, context, args) { |
| 89 | + Radio._callHandler = function(callback, context, args) { |
86 | 90 | var a1 = args[0], a2 = args[1], a3 = args[2]; |
87 | 91 | switch(args.length) { |
88 | 92 | case 0: return callback.call(context); |
|
91 | 95 | case 3: return callback.call(context, a1, a2, a3); |
92 | 96 | default: return callback.apply(context, args); |
93 | 97 | } |
94 | | - } |
| 98 | + }; |
95 | 99 |
|
96 | 100 | // A helper used by `off` methods to the handler from the store |
97 | 101 | function removeHandler(store, name, callback, context) { |
|
182 | 186 | // Issue a command |
183 | 187 | command: function(name) { |
184 | 188 | var args = _.rest(arguments); |
185 | | - if (eventsApi(this, 'command', name, args)) { |
| 189 | + if (Radio._eventsApi(this, 'command', name, args)) { |
186 | 190 | return this; |
187 | 191 | } |
188 | 192 | var channelName = this.channelName; |
|
197 | 201 | if (commands && (commands[name] || commands['default'])) { |
198 | 202 | var handler = commands[name] || commands['default']; |
199 | 203 | args = commands[name] ? args : arguments; |
200 | | - callHandler(handler.callback, handler.context, args); |
| 204 | + Radio._callHandler(handler.callback, handler.context, args); |
201 | 205 | } else { |
202 | | - debugLog('An unhandled command was fired', name, channelName); |
| 206 | + Radio.debugLog('An unhandled command was fired', name, channelName); |
203 | 207 | } |
204 | 208 |
|
205 | 209 | return this; |
206 | 210 | }, |
207 | 211 |
|
208 | 212 | // Register a handler for a command. |
209 | 213 | comply: function(name, callback, context) { |
210 | | - if (eventsApi(this, 'comply', name, [callback, context])) { |
| 214 | + if (Radio._eventsApi(this, 'comply', name, [callback, context])) { |
211 | 215 | return this; |
212 | 216 | } |
213 | 217 | this._commands || (this._commands = {}); |
214 | 218 |
|
215 | 219 | if (this._commands[name]) { |
216 | | - debugLog('A command was overwritten', name, this.channelName); |
| 220 | + Radio.debugLog('A command was overwritten', name, this.channelName); |
217 | 221 | } |
218 | 222 |
|
219 | 223 | this._commands[name] = { |
|
226 | 230 |
|
227 | 231 | // Register a handler for a command that happens just once. |
228 | 232 | complyOnce: function(name, callback, context) { |
229 | | - if (eventsApi(this, 'complyOnce', name, [callback, context])) { |
| 233 | + if (Radio._eventsApi(this, 'complyOnce', name, [callback, context])) { |
230 | 234 | return this; |
231 | 235 | } |
232 | 236 | var self = this; |
|
241 | 245 |
|
242 | 246 | // Remove handler(s) |
243 | 247 | stopComplying: function(name, callback, context) { |
244 | | - if (eventsApi(this, 'stopComplying', name)) { |
| 248 | + if (Radio._eventsApi(this, 'stopComplying', name)) { |
245 | 249 | return this; |
246 | 250 | } |
247 | 251 |
|
248 | 252 | // Remove everything if there are no arguments passed |
249 | 253 | if (!name && !callback && !context) { |
250 | 254 | delete this._commands; |
251 | 255 | } else if (!removeHandlers(this._commands, name, callback, context)) { |
252 | | - debugLog('Attempted to remove the unregistered command', name, this.channelName); |
| 256 | + Radio.debugLog('Attempted to remove the unregistered command', name, this.channelName); |
253 | 257 | } |
254 | 258 |
|
255 | 259 | return this; |
|
272 | 276 | // Make a request |
273 | 277 | request: function(name) { |
274 | 278 | var args = _.rest(arguments); |
275 | | - var results = eventsApi(this, 'request', name, args); |
| 279 | + var results = Radio._eventsApi(this, 'request', name, args); |
276 | 280 | if (results) { |
277 | 281 | return results; |
278 | 282 | } |
|
288 | 292 | if (requests && (requests[name] || requests['default'])) { |
289 | 293 | var handler = requests[name] || requests['default']; |
290 | 294 | args = requests[name] ? args : arguments; |
291 | | - return callHandler(handler.callback, handler.context, args); |
| 295 | + return Radio._callHandler(handler.callback, handler.context, args); |
292 | 296 | } else { |
293 | | - debugLog('An unhandled request was fired', name, channelName); |
| 297 | + Radio.debugLog('An unhandled request was fired', name, channelName); |
294 | 298 | } |
295 | 299 | }, |
296 | 300 |
|
297 | 301 | // Set up a handler for a request |
298 | 302 | reply: function(name, callback, context) { |
299 | | - if (eventsApi(this, 'reply', name, [callback, context])) { |
| 303 | + if (Radio._eventsApi(this, 'reply', name, [callback, context])) { |
300 | 304 | return this; |
301 | 305 | } |
302 | 306 |
|
303 | 307 | this._requests || (this._requests = {}); |
304 | 308 |
|
305 | 309 | if (this._requests[name]) { |
306 | | - debugLog('A request was overwritten', name, this.channelName); |
| 310 | + Radio.debugLog('A request was overwritten', name, this.channelName); |
307 | 311 | } |
308 | 312 |
|
309 | 313 | this._requests[name] = { |
|
316 | 320 |
|
317 | 321 | // Set up a handler that can only be requested once |
318 | 322 | replyOnce: function(name, callback, context) { |
319 | | - if (eventsApi(this, 'replyOnce', name, [callback, context])) { |
| 323 | + if (Radio._eventsApi(this, 'replyOnce', name, [callback, context])) { |
320 | 324 | return this; |
321 | 325 | } |
322 | 326 |
|
|
332 | 336 |
|
333 | 337 | // Remove handler(s) |
334 | 338 | stopReplying: function(name, callback, context) { |
335 | | - if (eventsApi(this, 'stopReplying', name)) { |
| 339 | + if (Radio._eventsApi(this, 'stopReplying', name)) { |
336 | 340 | return this; |
337 | 341 | } |
338 | 342 |
|
339 | 343 | // Remove everything if there are no arguments passed |
340 | 344 | if (!name && !callback && !context) { |
341 | 345 | delete this._requests; |
342 | 346 | } else if (!removeHandlers(this._requests, name, callback, context)) { |
343 | | - debugLog('Attempted to remove the unregistered request', name, this.channelName); |
| 347 | + Radio.debugLog('Attempted to remove the unregistered request', name, this.channelName); |
344 | 348 | } |
345 | 349 |
|
346 | 350 | return this; |
|
412 | 416 | }); |
413 | 417 | }); |
414 | 418 |
|
| 419 | + Radio.reset = function(channelName) { |
| 420 | + var channels = !channelName ? this._channels : [this._channels[channelName]]; |
| 421 | + _.invoke(channels, 'reset'); |
| 422 | + }; |
| 423 | + |
415 | 424 |
|
416 | 425 | return Radio; |
417 | 426 | })); |
0 commit comments