gfs-react-redux-twoway-binding
Version:
182 lines (170 loc) • 5.98 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else {
var a = factory();
for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];
}
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
module.exports = __webpack_require__(2);
/***/ },
/* 1 */,
/* 2 */
/***/ function(module, exports, __webpack_require__) {
"use strict";
var bindingMixin_1 = __webpack_require__(3);
exports.bindingMixin = bindingMixin_1["default"];
var createReducer_1 = __webpack_require__(4);
exports.createReducer = createReducer_1["default"];
/***/ },
/* 3 */
/***/ function(module, exports) {
"use strict";
function bindingMixin(DecoratedComponent) {
/**
* Two ways binding func.
* @param path
* @param value
* @returns {{value: (any|*), requestChange: Function}}
*/
DecoratedComponent.prototype.binding = function (path, transValueFunc) {
var reducerPath = this.bindingOrigin.reducerPath;
var dispatch = window['dispatch'] || this.state.dispatch || this.props.dispatch;
//可能会操作多个不同的store user,index,list...
var reducerPathArray = reducerPath.split('.');
//拿最后一个[user,index,list] = 'list'
var reducerName = reducerPathArray[reducerPathArray.length - 1];
var store = this.props[reducerName];
var topicName = reducerPathArray.shift();
var readPath = path.split('.');
var pathArray = reducerPathArray.concat(readPath);
var value = store.getIn(readPath);
if (transValueFunc) {
value = transValueFunc(value);
}
var valueLink = {
//path string like 'a.b.0'
value: value,
requestChange: function (newValue) {
dispatch({ type: topicName + "_BINDING_UPDATE", path: pathArray, value: newValue });
}
};
return valueLink;
};
/**
* Two ways binding setStore first
* @param store
*/
DecoratedComponent.prototype.setBinding = function (reducerPath) {
this.bindingOrigin = {
reducerPath: reducerPath
};
};
/**
* manual change value by path,newValue or a cover function
* @param path
* @param newValue
*/
DecoratedComponent.prototype.manualChange = function (path, newValue) {
var reducerName, store;
var reducerPath = this.bindingOrigin.reducerPath;
var reducerPathArray = reducerPath.split('.');
var dispatch = window['dispatch'] || this.state.dispatch || this.props.dispatch;
if (typeof newValue === "function") {
reducerName = reducerPathArray[reducerPathArray.length - 1];
store = this.props[reducerName];
}
var topicName = reducerPathArray.shift();
var pathArray = reducerPathArray.concat(path.split('.'));
if (typeof newValue === "function") {
var value = store.getIn(pathArray);
var alterValue = newValue(value);
dispatch({ type: topicName + "_BINDING_UPDATE", path: pathArray, value: alterValue });
return;
}
dispatch({ type: topicName + "_BINDING_UPDATE", path: pathArray, value: newValue });
};
}
exports.__esModule = true;
exports["default"] = bindingMixin;
;
/***/ },
/* 4 */
/***/ function(module, exports) {
"use strict";
var __assign = (this && this.__assign) || Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
function createReducer(reducerName, initialState, handlers) {
if (arguments.length <= 2) {
handlers = __assign({}, initialState);
initialState = reducerName;
}
handlers[reducerName + '_BINDING_UPDATE'] = function (data, action) {
return data.setIn(action.path, action.value);
};
return function (state, action) {
if (state === void 0) { state = initialState; }
return handlers[action.type] ?
handlers[action.type](state, action) :
state;
};
}
exports.__esModule = true;
exports["default"] = createReducer;
/***/ }
/******/ ])
});
;