marionettejs/backbone.radio

View on GitHub
build/backbone.radio.js.map

Summary

Maintainability
Test Coverage
{"version":3,"names":[],"mappings":"","sources":["backbone.radio.js"],"sourcesContent":["// Backbone.Radio v2.0.0\n\n(function (global, factory) {\n  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('underscore'), require('backbone')) :\n  typeof define === 'function' && define.amd ? define(['underscore', 'backbone'], factory) :\n  (global.Backbone = global.Backbone || {}, global.Backbone.Radio = factory(global._,global.Backbone));\n}(this, function (_,Backbone) { 'use strict';\n\n  _ = 'default' in _ ? _['default'] : _;\n  Backbone = 'default' in Backbone ? Backbone['default'] : Backbone;\n\n  var _typeof = typeof Symbol === \"function\" && typeof Symbol.iterator === \"symbol\" ? function (obj) {\n    return typeof obj;\n  } : function (obj) {\n    return obj && typeof Symbol === \"function\" && obj.constructor === Symbol ? \"symbol\" : typeof obj;\n  };\n\n  var previousRadio = Backbone.Radio;\n\n  var Radio = Backbone.Radio = {};\n\n  Radio.VERSION = '2.0.0';\n\n  // This allows you to run multiple instances of Radio on the same\n  // webapp. After loading the new version, call `noConflict()` to\n  // get a reference to it. At the same time the old version will be\n  // returned to Backbone.Radio.\n  Radio.noConflict = function () {\n    Backbone.Radio = previousRadio;\n    return this;\n  };\n\n  // Whether or not we're in DEBUG mode or not. DEBUG mode helps you\n  // get around the issues of lack of warnings when events are mis-typed.\n  Radio.DEBUG = false;\n\n  // Format debug text.\n  Radio._debugText = function (warning, eventName, channelName) {\n    return warning + (channelName ? ' on the ' + channelName + ' channel' : '') + ': \"' + eventName + '\"';\n  };\n\n  // This is the method that's called when an unregistered event was called.\n  // By default, it logs warning to the console. By overriding this you could\n  // make it throw an Error, for instance. This would make firing a nonexistent event\n  // have the same consequence as firing a nonexistent method on an Object.\n  Radio.debugLog = function (warning, eventName, channelName) {\n    if (Radio.DEBUG && console && console.warn) {\n      console.warn(Radio._debugText(warning, eventName, channelName));\n    }\n  };\n\n  var eventSplitter = /\\s+/;\n\n  // An internal method used to handle Radio's method overloading for Requests.\n  // It's borrowed from Backbone.Events. It differs from Backbone's overload\n  // API (which is used in Backbone.Events) in that it doesn't support space-separated\n  // event names.\n  Radio._eventsApi = function (obj, action, name, rest) {\n    if (!name) {\n      return false;\n    }\n\n    var results = {};\n\n    // Handle event maps.\n    if ((typeof name === 'undefined' ? 'undefined' : _typeof(name)) === 'object') {\n      for (var key in name) {\n        var result = obj[action].apply(obj, [key, name[key]].concat(rest));\n        eventSplitter.test(key) ? _.extend(results, result) : results[key] = result;\n      }\n      return results;\n    }\n\n    // Handle space separated event names.\n    if (eventSplitter.test(name)) {\n      var names = name.split(eventSplitter);\n      for (var i = 0, l = names.length; i < l; i++) {\n        results[names[i]] = obj[action].apply(obj, [names[i]].concat(rest));\n      }\n      return results;\n    }\n\n    return false;\n  };\n\n  // An optimized way to execute callbacks.\n  Radio._callHandler = function (callback, context, args) {\n    var a1 = args[0],\n        a2 = args[1],\n        a3 = args[2];\n    switch (args.length) {\n      case 0:\n        return callback.call(context);\n      case 1:\n        return callback.call(context, a1);\n      case 2:\n        return callback.call(context, a1, a2);\n      case 3:\n        return callback.call(context, a1, a2, a3);\n      default:\n        return callback.apply(context, args);\n    }\n  };\n\n  // A helper used by `off` methods to the handler from the store\n  function removeHandler(store, name, callback, context) {\n    var event = store[name];\n    if ((!callback || callback === event.callback || callback === event.callback._callback) && (!context || context === event.context)) {\n      delete store[name];\n      return true;\n    }\n  }\n\n  function removeHandlers(store, name, callback, context) {\n    store || (store = {});\n    var names = name ? [name] : _.keys(store);\n    var matched = false;\n\n    for (var i = 0, length = names.length; i < length; i++) {\n      name = names[i];\n\n      // If there's no event by this name, log it and continue\n      // with the loop\n      if (!store[name]) {\n        continue;\n      }\n\n      if (removeHandler(store, name, callback, context)) {\n        matched = true;\n      }\n    }\n\n    return matched;\n  }\n\n  /*\n   * tune-in\n   * -------\n   * Get console logs of a channel's activity\n   *\n   */\n\n  var _logs = {};\n\n  // This is to produce an identical function in both tuneIn and tuneOut,\n  // so that Backbone.Events unregisters it.\n  function _partial(channelName) {\n    return _logs[channelName] || (_logs[channelName] = _.bind(Radio.log, Radio, channelName));\n  }\n\n  _.extend(Radio, {\n\n    // Log information about the channel and event\n    log: function log(channelName, eventName) {\n      if (typeof console === 'undefined') {\n        return;\n      }\n      var args = _.toArray(arguments).slice(2);\n      console.log('[' + channelName + '] \"' + eventName + '\"', args);\n    },\n\n    // Logs all events on this channel to the console. It sets an\n    // internal value on the channel telling it we're listening,\n    // then sets a listener on the Backbone.Events\n    tuneIn: function tuneIn(channelName) {\n      var channel = Radio.channel(channelName);\n      channel._tunedIn = true;\n      channel.on('all', _partial(channelName));\n      return this;\n    },\n\n    // Stop logging all of the activities on this channel to the console\n    tuneOut: function tuneOut(channelName) {\n      var channel = Radio.channel(channelName);\n      channel._tunedIn = false;\n      channel.off('all', _partial(channelName));\n      delete _logs[channelName];\n      return this;\n    }\n  });\n\n  /*\n   * Backbone.Radio.Requests\n   * -----------------------\n   * A messaging system for requesting data.\n   *\n   */\n\n  function makeCallback(callback) {\n    return _.isFunction(callback) ? callback : function () {\n      return callback;\n    };\n  }\n\n  Radio.Requests = {\n\n    // Make a request\n    request: function request(name) {\n      var args = _.toArray(arguments).slice(1);\n      var results = Radio._eventsApi(this, 'request', name, args);\n      if (results) {\n        return results;\n      }\n      var channelName = this.channelName;\n      var requests = this._requests;\n\n      // Check if we should log the request, and if so, do it\n      if (channelName && this._tunedIn) {\n        Radio.log.apply(this, [channelName, name].concat(args));\n      }\n\n      // If the request isn't handled, log it in DEBUG mode and exit\n      if (requests && (requests[name] || requests['default'])) {\n        var handler = requests[name] || requests['default'];\n        args = requests[name] ? args : arguments;\n        return Radio._callHandler(handler.callback, handler.context, args);\n      } else {\n        Radio.debugLog('An unhandled request was fired', name, channelName);\n      }\n    },\n\n    // Set up a handler for a request\n    reply: function reply(name, callback, context) {\n      if (Radio._eventsApi(this, 'reply', name, [callback, context])) {\n        return this;\n      }\n\n      this._requests || (this._requests = {});\n\n      if (this._requests[name]) {\n        Radio.debugLog('A request was overwritten', name, this.channelName);\n      }\n\n      this._requests[name] = {\n        callback: makeCallback(callback),\n        context: context || this\n      };\n\n      return this;\n    },\n\n    // Set up a handler that can only be requested once\n    replyOnce: function replyOnce(name, callback, context) {\n      if (Radio._eventsApi(this, 'replyOnce', name, [callback, context])) {\n        return this;\n      }\n\n      var self = this;\n\n      var once = _.once(function () {\n        self.stopReplying(name);\n        return makeCallback(callback).apply(this, arguments);\n      });\n\n      return this.reply(name, once, context);\n    },\n\n    // Remove handler(s)\n    stopReplying: function stopReplying(name, callback, context) {\n      if (Radio._eventsApi(this, 'stopReplying', name)) {\n        return this;\n      }\n\n      // Remove everything if there are no arguments passed\n      if (!name && !callback && !context) {\n        delete this._requests;\n      } else if (!removeHandlers(this._requests, name, callback, context)) {\n        Radio.debugLog('Attempted to remove the unregistered request', name, this.channelName);\n      }\n\n      return this;\n    }\n  };\n\n  /*\n   * Backbone.Radio.channel\n   * ----------------------\n   * Get a reference to a channel by name.\n   *\n   */\n\n  Radio._channels = {};\n\n  Radio.channel = function (channelName) {\n    if (!channelName) {\n      throw new Error('You must provide a name for the channel.');\n    }\n\n    if (Radio._channels[channelName]) {\n      return Radio._channels[channelName];\n    } else {\n      return Radio._channels[channelName] = new Radio.Channel(channelName);\n    }\n  };\n\n  /*\n   * Backbone.Radio.Channel\n   * ----------------------\n   * A Channel is an object that extends from Backbone.Events,\n   * and Radio.Requests.\n   *\n   */\n\n  Radio.Channel = function (channelName) {\n    this.channelName = channelName;\n  };\n\n  _.extend(Radio.Channel.prototype, Backbone.Events, Radio.Requests, {\n\n    // Remove all handlers from the messaging systems of this channel\n    reset: function reset() {\n      this.off();\n      this.stopListening();\n      this.stopReplying();\n      return this;\n    }\n  });\n\n  /*\n   * Top-level API\n   * -------------\n   * Supplies the 'top-level API' for working with Channels directly\n   * from Backbone.Radio.\n   *\n   */\n\n  var channel;\n  var args;\n  var systems = [Backbone.Events, Radio.Requests];\n  _.each(systems, function (system) {\n    _.each(system, function (method, methodName) {\n      Radio[methodName] = function (channelName) {\n        args = _.toArray(arguments).slice(1);\n        channel = this.channel(channelName);\n        return channel[methodName].apply(channel, args);\n      };\n    });\n  });\n\n  Radio.reset = function (channelName) {\n    var channels = !channelName ? this._channels : [this._channels[channelName]];\n    _.each(channels, function (channel) {\n      channel.reset();\n    });\n  };\n\n  return Radio;\n\n}));\n"],"file":"backbone.radio.js","sourceRoot":"/source/"}