yylib-quick-mobile
Version:
yylib-quick-mobile
71 lines (61 loc) • 2.41 kB
JavaScript
var _require = require('react-redux'),
_connect = _require.connect;
var isFunction = require('lodash/isFunction');
var assign = require('lodash/assign');
function _mapDispatchToProps(dispatch) {
return {
dispatch: dispatch,
sendAction: function sendAction() {
dispatch(ReduxUtils.createAction.apply(this, arguments));
}
};
}
var ReduxUtils = {
ACTION_KEY: "__REDUX_SEND_ACTION_DATA__",
UNKNOW_ACTION_KEY: "__REDUX_SEND_ACTION_UNKNOW__",
connect: function connect(reactClass, mapStateToProps, mapDispatchToProps) {
return _connect(mapStateToProps, function (dispatch) {
var _props = _mapDispatchToProps(dispatch);
if (isFunction(mapDispatchToProps)) {
_props = assign(_props, mapDispatchToProps(dispatch));
}
return _props;
})(reactClass);
},
createAction: function createAction(key) {
var actionKey = arguments[0];
if (!actionKey) {
console.error('调用sendAction方法,需要将动作指令名称{string}作为第一个参数值');
return { "type": ReduxUtils.UNKNOW_ACTION_KEY };
}
var actionArgs = [];
for (var i = 1; i < arguments.length; i++) {
actionArgs.push(arguments[i]);
}
var newAction = {};
newAction.type = ReduxUtils.ACTION_KEY;
newAction.actionKey = actionKey;
newAction.actionArgs = actionArgs;
return newAction;
},
listen: function listen(actions) {
return function () {
var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var action = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { type: type, actionKey: actionKey, actionArgs: [] };
if (action.type == ReduxUtils.ACTION_KEY) {
var actionFunc = actions[action.actionKey];
if (isFunction(actionFunc)) {
var callArgs = [state].concat(action.actionArgs);
var newState = actionFunc.apply(this, callArgs);
return newState == undefined ? state : newState;
} else {
return state;
}
} else {
return state;
}
};
}
};
module.exports = ReduxUtils;
;