Code coverage report for lib/util/connect.js

Statements: 100% (12 / 12)      Branches: 100% (2 / 2)      Functions: 100% (3 / 3)      Lines: 100% (12 / 12)      Ignored: none     

All files » lib/util/ » connect.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                      1 1   1 1 1                     1   2               2   2                 2                                 2       1  
/**
 * @file 包裹React组件,返回一个新组件,新组件可以访问`ei`上下文,从而提供`store`数据和`dispatch`方法
 *
 * 原理是使用high order组件
 *
 * @author Leon(leon@outlook.com)
 *
 * @requires react
 * @requires underscore
 */
 
var React = require('react');
var u = require('underscore');
 
var ContextConnector = require('../component/ContextConnector');
var bindActions = require('./bindActions');
var bindSelectors = require('./bindSelectors');
 
/**
 * 使用high order方法,包装一个组件,使它具体连接到context的能力
 *
 * @method module:ei.connect
 * @param {!ReactComponent}      Component 指定的组件
 * @param {*}                    selector  数据选择器,支持多种方式选择数据
 * @param {?Map<string, Object>} actions   名称 - ActionCreator
 * @return {ReactComponent}
 */
function connect(Component, selector, actions) {
 
    var ContextFixer = React.createClass({
 
        displayName: 'ContextFixer',
 
        select: bindSelectors(selector),
 
        render: function () {
 
            var props = this.props;
 
            return React.createElement(
 
                ContextConnector,
 
                {
                    select: this.select
                },
 
                function (state, dispatch) {
                    return React.createElement(
                        Component,
                        u.extendOwn(
                            {},
                            state,
                            props,
                            actions ? bindActions(dispatch, actions) : null
                        )
                    );
                }
 
            );
 
        }
 
    });
 
    return ContextFixer;
 
}
 
module.exports = connect;