# All files / composer dispatcher.js

93.33% Statements 14/15
100% Branches 2/2
66.67% Functions 2/3
93.33% Lines 14/15

    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

    8x

    8x

    8x

    8x
    32x
    48x
    48x
    48x

    48x
    47x

    1x

    8x
    32x

    8x
     // TODO: clean up w/ newer syntax, and perhaps use vanilla Flux dispatcher from Facebook instead

    var Dispatcher = require('flux').Dispatcher;
    // buffer.data.profiles

    /**
     * Testing the idea of using an event PubSub or "Dispatcher".
     * Using this to decouple UI events and changes in Models + Collections.
     *
     *   http://facebook.github.io/flux/docs/dispatcher.html
     *
     * Example dispatch with a payload
     *
     *   AppDispatcher.handleViewAction({
     *     actionType: ActionTypes.SELECT_PROFILE,
     *     id: '5463a800b0e6df64097b23c6'
     *   });
     *
     */

    var AppDispatcher = new Dispatcher();

    var METHODS = {
      'handleViewAction': 'VIEW_ACTION',
      'handleRouterAction': 'ROUTER_ACTION',
      'handleApiAction': 'API_ACTION',
      'handlePusherAction': 'PUSHER_ACTION'
    };

    var createHandler = function(source) {
      return function(action) {
        var _this = this;
        var isDispatching = AppDispatcher.isDispatching();
        var dispatchOptions = {
          source: source,
          action: action
        };

        // If the dispatcher is currently dispatching
        // then add the next dispatch into the event queue
        if (isDispatching) {
          setTimeout(function(){
            _this.dispatch(dispatchOptions);
          }, 0);
        } else {
          this.dispatch(dispatchOptions);
        }
      };
    };

    for (var method in METHODS) {
      AppDispatcher[method] = createHandler(METHODS[method]);
    }

    module.exports = AppDispatcher;