Code coverage report for src/wreqr.channel.js

Statements: 96% (24 / 25)      Branches: 83.33% (5 / 6)      Functions: 100% (8 / 8)      Lines: 96% (24 / 25)     

All files » src/ » wreqr.channel.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          1     1 7 7 7 7     1       8 8 8 8 8         2 2       2 2       2 2         6       6 6   6 12           1    
// Wreqr.Channel
// --------------
//
// An object that wraps the three messaging systems:
// EventAggregator, RequestResponse, Commands
Wreqr.Channel = (function(Wreqr){
  "use strict";
 
  var Channel = function(channelName) {
    this.vent        = new Backbone.Wreqr.EventAggregator();
    this.reqres      = new Backbone.Wreqr.RequestResponse();
    this.commands    = new Backbone.Wreqr.Commands();
    this.channelName = channelName;
  };
 
  _.extend(Channel.prototype, {
 
    // Remove all handlers from the messaging systems of this channel
    reset: function() {
      this.vent.off();
      this.vent.stopListening();
      this.reqres.removeAllHandlers();
      this.commands.removeAllHandlers();
      return this;
    },
 
    // Connect a hash of events; one for each messaging system
    connectEvents: function(hash, context) {
      this._connect('vent', hash, context);
      return this;
    },
 
    connectCommands: function(hash, context) {
      this._connect('commands', hash, context);
      return this;
    },
 
    connectRequests: function(hash, context) {
      this._connect('reqres', hash, context);
      return this;
    },
 
    // Attach the handlers to a given message system `type`
    _connect: function(type, hash, context) {
      Iif (!hash) {
        return;
      }
 
      context = context || this;
      var method = (type === 'vent') ? 'on' : 'setHandler';
 
      _.each(hash, function(fn, eventName) {
        this[type][method](eventName, _.bind(fn, context));
      }, this);
    }
  });
 
 
  return Channel;
})(Wreqr);