backbone.marionette.js
8 years ago
backbone.marionette.js.map
8 years ago
backbone.marionette.min.js
8 years ago
backbone.marionette.min.js.map
8 years ago
backbone.radio.js
8 years ago
backbone.radio.js.map
8 years ago
backbone.radio.min.js
7 years ago
backbone.radio.min.js.map
7 years ago
backbone.radio.js
350 lines
| 1 | // Backbone.Radio v2.0.0 |
| 2 | |
| 3 | (function (global, factory) { |
| 4 | typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('underscore'), require('backbone')) : |
| 5 | typeof define === 'function' && define.amd ? define(['underscore', 'backbone'], factory) : |
| 6 | (global.Backbone = global.Backbone || {}, global.Backbone.Radio = factory(global._,global.Backbone)); |
| 7 | }(this, function (_,Backbone) { 'use strict'; |
| 8 | |
| 9 | _ = 'default' in _ ? _['default'] : _; |
| 10 | Backbone = 'default' in Backbone ? Backbone['default'] : Backbone; |
| 11 | |
| 12 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { |
| 13 | return typeof obj; |
| 14 | } : function (obj) { |
| 15 | return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; |
| 16 | }; |
| 17 | |
| 18 | var previousRadio = Backbone.Radio; |
| 19 | |
| 20 | var Radio = Backbone.Radio = {}; |
| 21 | |
| 22 | Radio.VERSION = '2.0.0'; |
| 23 | |
| 24 | // This allows you to run multiple instances of Radio on the same |
| 25 | // webapp. After loading the new version, call `noConflict()` to |
| 26 | // get a reference to it. At the same time the old version will be |
| 27 | // returned to Backbone.Radio. |
| 28 | Radio.noConflict = function () { |
| 29 | Backbone.Radio = previousRadio; |
| 30 | return this; |
| 31 | }; |
| 32 | |
| 33 | // Whether or not we're in DEBUG mode or not. DEBUG mode helps you |
| 34 | // get around the issues of lack of warnings when events are mis-typed. |
| 35 | Radio.DEBUG = false; |
| 36 | |
| 37 | // Format debug text. |
| 38 | Radio._debugText = function (warning, eventName, channelName) { |
| 39 | return warning + (channelName ? ' on the ' + channelName + ' channel' : '') + ': "' + eventName + '"'; |
| 40 | }; |
| 41 | |
| 42 | // This is the method that's called when an unregistered event was called. |
| 43 | // By default, it logs warning to the console. By overriding this you could |
| 44 | // make it throw an Error, for instance. This would make firing a nonexistent event |
| 45 | // have the same consequence as firing a nonexistent method on an Object. |
| 46 | Radio.debugLog = function (warning, eventName, channelName) { |
| 47 | if (Radio.DEBUG && console && console.warn) { |
| 48 | console.warn(Radio._debugText(warning, eventName, channelName)); |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | var eventSplitter = /\s+/; |
| 53 | |
| 54 | // An internal method used to handle Radio's method overloading for Requests. |
| 55 | // It's borrowed from Backbone.Events. It differs from Backbone's overload |
| 56 | // API (which is used in Backbone.Events) in that it doesn't support space-separated |
| 57 | // event names. |
| 58 | Radio._eventsApi = function (obj, action, name, rest) { |
| 59 | if (!name) { |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | var results = {}; |
| 64 | |
| 65 | // Handle event maps. |
| 66 | if ((typeof name === 'undefined' ? 'undefined' : _typeof(name)) === 'object') { |
| 67 | for (var key in name) { |
| 68 | var result = obj[action].apply(obj, [key, name[key]].concat(rest)); |
| 69 | eventSplitter.test(key) ? _.extend(results, result) : results[key] = result; |
| 70 | } |
| 71 | return results; |
| 72 | } |
| 73 | |
| 74 | // Handle space separated event names. |
| 75 | if (eventSplitter.test(name)) { |
| 76 | var names = name.split(eventSplitter); |
| 77 | for (var i = 0, l = names.length; i < l; i++) { |
| 78 | results[names[i]] = obj[action].apply(obj, [names[i]].concat(rest)); |
| 79 | } |
| 80 | return results; |
| 81 | } |
| 82 | |
| 83 | return false; |
| 84 | }; |
| 85 | |
| 86 | // An optimized way to execute callbacks. |
| 87 | Radio._callHandler = function (callback, context, args) { |
| 88 | var a1 = args[0], |
| 89 | a2 = args[1], |
| 90 | a3 = args[2]; |
| 91 | switch (args.length) { |
| 92 | case 0: |
| 93 | return callback.call(context); |
| 94 | case 1: |
| 95 | return callback.call(context, a1); |
| 96 | case 2: |
| 97 | return callback.call(context, a1, a2); |
| 98 | case 3: |
| 99 | return callback.call(context, a1, a2, a3); |
| 100 | default: |
| 101 | return callback.apply(context, args); |
| 102 | } |
| 103 | }; |
| 104 | |
| 105 | // A helper used by `off` methods to the handler from the store |
| 106 | function removeHandler(store, name, callback, context) { |
| 107 | var event = store[name]; |
| 108 | if ((!callback || callback === event.callback || callback === event.callback._callback) && (!context || context === event.context)) { |
| 109 | delete store[name]; |
| 110 | return true; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | function removeHandlers(store, name, callback, context) { |
| 115 | store || (store = {}); |
| 116 | var names = name ? [name] : _.keys(store); |
| 117 | var matched = false; |
| 118 | |
| 119 | for (var i = 0, length = names.length; i < length; i++) { |
| 120 | name = names[i]; |
| 121 | |
| 122 | // If there's no event by this name, log it and continue |
| 123 | // with the loop |
| 124 | if (!store[name]) { |
| 125 | continue; |
| 126 | } |
| 127 | |
| 128 | if (removeHandler(store, name, callback, context)) { |
| 129 | matched = true; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | return matched; |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * tune-in |
| 138 | * ------- |
| 139 | * Get console logs of a channel's activity |
| 140 | * |
| 141 | */ |
| 142 | |
| 143 | var _logs = {}; |
| 144 | |
| 145 | // This is to produce an identical function in both tuneIn and tuneOut, |
| 146 | // so that Backbone.Events unregisters it. |
| 147 | function _partial(channelName) { |
| 148 | return _logs[channelName] || (_logs[channelName] = _.bind(Radio.log, Radio, channelName)); |
| 149 | } |
| 150 | |
| 151 | _.extend(Radio, { |
| 152 | |
| 153 | // Log information about the channel and event |
| 154 | log: function log(channelName, eventName) { |
| 155 | if (typeof console === 'undefined') { |
| 156 | return; |
| 157 | } |
| 158 | var args = _.toArray(arguments).slice(2); |
| 159 | console.log('[' + channelName + '] "' + eventName + '"', args); |
| 160 | }, |
| 161 | |
| 162 | // Logs all events on this channel to the console. It sets an |
| 163 | // internal value on the channel telling it we're listening, |
| 164 | // then sets a listener on the Backbone.Events |
| 165 | tuneIn: function tuneIn(channelName) { |
| 166 | var channel = Radio.channel(channelName); |
| 167 | channel._tunedIn = true; |
| 168 | channel.on('all', _partial(channelName)); |
| 169 | return this; |
| 170 | }, |
| 171 | |
| 172 | // Stop logging all of the activities on this channel to the console |
| 173 | tuneOut: function tuneOut(channelName) { |
| 174 | var channel = Radio.channel(channelName); |
| 175 | channel._tunedIn = false; |
| 176 | channel.off('all', _partial(channelName)); |
| 177 | delete _logs[channelName]; |
| 178 | return this; |
| 179 | } |
| 180 | }); |
| 181 | |
| 182 | /* |
| 183 | * Backbone.Radio.Requests |
| 184 | * ----------------------- |
| 185 | * A messaging system for requesting data. |
| 186 | * |
| 187 | */ |
| 188 | |
| 189 | function makeCallback(callback) { |
| 190 | return _.isFunction(callback) ? callback : function () { |
| 191 | return callback; |
| 192 | }; |
| 193 | } |
| 194 | |
| 195 | Radio.Requests = { |
| 196 | |
| 197 | // Make a request |
| 198 | request: function request(name) { |
| 199 | var args = _.toArray(arguments).slice(1); |
| 200 | var results = Radio._eventsApi(this, 'request', name, args); |
| 201 | if (results) { |
| 202 | return results; |
| 203 | } |
| 204 | var channelName = this.channelName; |
| 205 | var requests = this._requests; |
| 206 | |
| 207 | // Check if we should log the request, and if so, do it |
| 208 | if (channelName && this._tunedIn) { |
| 209 | Radio.log.apply(this, [channelName, name].concat(args)); |
| 210 | } |
| 211 | |
| 212 | // If the request isn't handled, log it in DEBUG mode and exit |
| 213 | if (requests && (requests[name] || requests['default'])) { |
| 214 | var handler = requests[name] || requests['default']; |
| 215 | args = requests[name] ? args : arguments; |
| 216 | return Radio._callHandler(handler.callback, handler.context, args); |
| 217 | } else { |
| 218 | Radio.debugLog('An unhandled request was fired', name, channelName); |
| 219 | } |
| 220 | }, |
| 221 | |
| 222 | // Set up a handler for a request |
| 223 | reply: function reply(name, callback, context) { |
| 224 | if (Radio._eventsApi(this, 'reply', name, [callback, context])) { |
| 225 | return this; |
| 226 | } |
| 227 | |
| 228 | this._requests || (this._requests = {}); |
| 229 | |
| 230 | if (this._requests[name]) { |
| 231 | Radio.debugLog('A request was overwritten', name, this.channelName); |
| 232 | } |
| 233 | |
| 234 | this._requests[name] = { |
| 235 | callback: makeCallback(callback), |
| 236 | context: context || this |
| 237 | }; |
| 238 | |
| 239 | return this; |
| 240 | }, |
| 241 | |
| 242 | // Set up a handler that can only be requested once |
| 243 | replyOnce: function replyOnce(name, callback, context) { |
| 244 | if (Radio._eventsApi(this, 'replyOnce', name, [callback, context])) { |
| 245 | return this; |
| 246 | } |
| 247 | |
| 248 | var self = this; |
| 249 | |
| 250 | var once = _.once(function () { |
| 251 | self.stopReplying(name); |
| 252 | return makeCallback(callback).apply(this, arguments); |
| 253 | }); |
| 254 | |
| 255 | return this.reply(name, once, context); |
| 256 | }, |
| 257 | |
| 258 | // Remove handler(s) |
| 259 | stopReplying: function stopReplying(name, callback, context) { |
| 260 | if (Radio._eventsApi(this, 'stopReplying', name)) { |
| 261 | return this; |
| 262 | } |
| 263 | |
| 264 | // Remove everything if there are no arguments passed |
| 265 | if (!name && !callback && !context) { |
| 266 | delete this._requests; |
| 267 | } else if (!removeHandlers(this._requests, name, callback, context)) { |
| 268 | Radio.debugLog('Attempted to remove the unregistered request', name, this.channelName); |
| 269 | } |
| 270 | |
| 271 | return this; |
| 272 | } |
| 273 | }; |
| 274 | |
| 275 | /* |
| 276 | * Backbone.Radio.channel |
| 277 | * ---------------------- |
| 278 | * Get a reference to a channel by name. |
| 279 | * |
| 280 | */ |
| 281 | |
| 282 | Radio._channels = {}; |
| 283 | |
| 284 | Radio.channel = function (channelName) { |
| 285 | if (!channelName) { |
| 286 | throw new Error('You must provide a name for the channel.'); |
| 287 | } |
| 288 | |
| 289 | if (Radio._channels[channelName]) { |
| 290 | return Radio._channels[channelName]; |
| 291 | } else { |
| 292 | return Radio._channels[channelName] = new Radio.Channel(channelName); |
| 293 | } |
| 294 | }; |
| 295 | |
| 296 | /* |
| 297 | * Backbone.Radio.Channel |
| 298 | * ---------------------- |
| 299 | * A Channel is an object that extends from Backbone.Events, |
| 300 | * and Radio.Requests. |
| 301 | * |
| 302 | */ |
| 303 | |
| 304 | Radio.Channel = function (channelName) { |
| 305 | this.channelName = channelName; |
| 306 | }; |
| 307 | |
| 308 | _.extend(Radio.Channel.prototype, Backbone.Events, Radio.Requests, { |
| 309 | |
| 310 | // Remove all handlers from the messaging systems of this channel |
| 311 | reset: function reset() { |
| 312 | this.off(); |
| 313 | this.stopListening(); |
| 314 | this.stopReplying(); |
| 315 | return this; |
| 316 | } |
| 317 | }); |
| 318 | |
| 319 | /* |
| 320 | * Top-level API |
| 321 | * ------------- |
| 322 | * Supplies the 'top-level API' for working with Channels directly |
| 323 | * from Backbone.Radio. |
| 324 | * |
| 325 | */ |
| 326 | |
| 327 | var channel; |
| 328 | var args; |
| 329 | var systems = [Backbone.Events, Radio.Requests]; |
| 330 | _.each(systems, function (system) { |
| 331 | _.each(system, function (method, methodName) { |
| 332 | Radio[methodName] = function (channelName) { |
| 333 | args = _.toArray(arguments).slice(1); |
| 334 | channel = this.channel(channelName); |
| 335 | return channel[methodName].apply(channel, args); |
| 336 | }; |
| 337 | }); |
| 338 | }); |
| 339 | |
| 340 | Radio.reset = function (channelName) { |
| 341 | var channels = !channelName ? this._channels : [this._channels[channelName]]; |
| 342 | _.each(channels, function (channel) { |
| 343 | channel.reset(); |
| 344 | }); |
| 345 | }; |
| 346 | |
| 347 | return Radio; |
| 348 | |
| 349 | })); |
| 350 | //# sourceMappingURL=./backbone.radio.js.map |