Code coverage report for lib/util/bindActions.js

Statements: 100% (11 / 11)      Branches: 100% (0 / 0)      Functions: 100% (3 / 3)      Lines: 100% (11 / 11)      Ignored: none     

All files » lib/util/ » bindActions.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          1   1                   1   4 2   2       3   2   2   1               1  
/**
 * @file bindActions
 * @author Leon(leon@outlook.com)
 */
 
var u = require('underscore');
 
var invariant = require('./invariant');
 
/**
 * 将一堆action creator绑定到一个`dispatch`方法上
 *
 * @private
 * @param  {!Function} dispatch dispatch方法
 * @param  {!Object}   actions  一个action creator的map
 * @return {Object}
 */
function bindActions(dispatch, actions) {
 
    invariant(actions, 'need action config');
    invariant(dispatch, 'need dispatch');
 
    return u.mapObject(
        actions,
        function (creator, methodName) {
 
            return function () {
 
                var action = creator.apply(null, arguments);
 
                invariant(action, 'action creator must return a object/funciton');
 
                return dispatch(action);
            };
 
        }
    );
 
}
 
module.exports = bindActions;