Code coverage report for src/wreqr.handlers.js

Statements: 96.3% (26 / 27)      Branches: 87.5% (7 / 8)      Functions: 100% (10 / 10)      Lines: 96.3% (26 / 27)     

All files » src/ » wreqr.handlers.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 88        1           1 36 36   36         1         1       3 5   5 1 1     5             28         28   28         12             17   17 4     13 13 13           1         17       1    
// Handlers
// --------
// A registry of functions to call, given a name
 
Wreqr.Handlers = (function(Backbone, _){
  "use strict";
  
  // Constructor
  // -----------
 
  var Handlers = function(options){
    this.options = options;
    this._wreqrHandlers = {};
    
    Iif (_.isFunction(this.initialize)){
      this.initialize(options);
    }
  };
 
  Handlers.extend = Backbone.Model.extend;
 
  // Instance Members
  // ----------------
 
  _.extend(Handlers.prototype, Backbone.Events, {
 
    // Add multiple handlers using an object literal configuration
    setHandlers: function(handlers){
      _.each(handlers, function(handler, name){
        var context = null;
 
        if (_.isObject(handler) && !_.isFunction(handler)){
          context = handler.context;
          handler = handler.callback;
        }
 
        this.setHandler(name, handler, context);
      }, this);
    },
 
    // Add a handler for the given name, with an
    // optional context to run the handler within
    setHandler: function(name, handler, context){
      var config = {
        callback: handler,
        context: context
      };
 
      this._wreqrHandlers[name] = config;
 
      this.trigger("handler:add", name, handler, context);
    },
 
    // Determine whether or not a handler is registered
    hasHandler: function(name){
      return !! this._wreqrHandlers[name];
    },
 
    // Get the currently registered handler for
    // the specified name. Throws an exception if
    // no handler is found.
    getHandler: function(name){
      var config = this._wreqrHandlers[name];
 
      if (!config){
        return;
      }
 
      return function(){
        var args = Array.prototype.slice.apply(arguments);
        return config.callback.apply(config.context, args);
      };
    },
 
    // Remove a handler for the specified name
    removeHandler: function(name){
      delete this._wreqrHandlers[name];
    },
 
    // Remove all handlers from this registry
    removeAllHandlers: function(){
      this._wreqrHandlers = {};
    }
  });
 
  return Handlers;
})(Backbone, _);