Code coverage report for lib/component/ContextConnector.js

Statements: 60% (6 / 10)      Branches: 100% (0 / 0)      Functions: 42.86% (3 / 7)      Lines: 60% (6 / 10)      Ignored: none     

All files » lib/component/ » ContextConnector.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            1                 1                           3       3                                                 3           1  
/**
 * @file 可访问`ei`上下文的组件
 * @author Leon(leon@outlook.com)
 * @requires react
 */
 
var React = require('react');
 
/**
 * `ei`上下文连接组件
 *
 * 凡是在`ContextProvider`中的组件都可以通过被此组件包裹后,获得到对`ei`上下文的访问功能
 *
 * @constructor
 */
var ContextConnector = React.createClass({
 
    displayName: 'ContextConnector',
 
    contextTypes: {
        ei: React.PropTypes.object.isRequired
    },
 
    propTypes: {
        children: React.PropTypes.func.isRequired,
        select: React.PropTypes.func.isRequired
    },
 
    getDataFromContext: function () {
        return this.props.select(this.context.ei.store);
    },
 
    getInitialState: function () {
        return {
            data: this.getDataFromContext()
        };
    },
 
    componentDidMount: function () {
        this.context.ei.addChangeListener(this.onStoreChange);
    },
 
    componentWillUnmount: function () {
        this.context.ei.removeChangeListener(this.onStoreChange);
    },
 
    shouldComponentUpdate: function (nextProps, nextState) {
        return this.state.data !== nextState;
    },
 
    onStoreChange: function () {
        this.setState({
            data: this.getDataFromContext()
        });
    },
 
    render: function () {
 
        return this.props.children(this.state.data, this.context.ei.dispatch);
 
    }
 
});
 
module.exports = ContextConnector;