Code coverage report for src/wreqr.radio.js

Statements: 100% (26 / 26)      Branches: 100% (4 / 4)      Functions: 100% (9 / 9)      Lines: 100% (26 / 26)     

All files » src/ » wreqr.radio.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87        1     1 1 1 1 1 1     1     42 1     41       75   75 3 3     75       1 3 17             1                                                       1 17 34 34   34       1      
// Wreqr.Radio
// --------------
//
// An object that lets you communicate with many channels.
Wreqr.radio = (function(Wreqr){
  "use strict";
 
  var Radio = function() {
    this._channels = {};
    this.vent = {};
    this.commands = {};
    this.reqres = {};
    this._proxyMethods();
  };
 
  _.extend(Radio.prototype, {
 
    channel: function(channelName) {
      if (!channelName) {
        throw new Error('Channel must receive a name');
      }
 
      return this._getChannel( channelName );
    },
 
    _getChannel: function(channelName) {
      var channel = this._channels[channelName];
 
      if(!channel) {
        channel = new Wreqr.Channel(channelName);
        this._channels[channelName] = channel;
      }
 
      return channel;
    },
 
    _proxyMethods: function() {
      _.each(['vent', 'commands', 'reqres'], function(system) {
        _.each( messageSystems[system], function(method) {
          this[system][method] = proxyMethod(this, system, method);
        }, this);
      }, this);
    }
  });
 
 
  var messageSystems = {
    vent: [
      'on',
      'off',
      'trigger',
      'once',
      'stopListening',
      'listenTo',
      'listenToOnce'
    ],
 
    commands: [
      'execute',
      'setHandler',
      'setHandlers',
      'removeHandler',
      'removeAllHandlers'
    ],
 
    reqres: [
      'request',
      'setHandler',
      'setHandlers',
      'removeHandler',
      'removeAllHandlers'
    ]
  };
 
  var proxyMethod = function(radio, system, method) {
    return function(channelName) {
      var messageSystem = radio._getChannel(channelName)[system];
      var args = Array.prototype.slice.call(arguments, 1);
 
      return messageSystem[method].apply(messageSystem, args);
    };
  };
 
  return new Radio();
 
})(Wreqr);