UNPKG

reactmvc

Version:

M(immutable model)V(view component)C(react-router)

182 lines (159 loc) 7.39 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } }; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * path: 'xxx.xxx.xxx' */ var Model = function () { function Model(routes) { _classCallCheck(this, Model); if (!routes || (typeof routes === 'undefined' ? 'undefined' : _typeof(routes)) !== 'object') { throw new Error('Model should have routes(object) parameter'); } this.model = routes; } _createClass(Model, [{ key: 'get', value: function get(path) { var pathArr = path.split('.'); var parent = this.model; pathArr.slice(0, -1).every(function (key) { parent = parent[key]; if ((typeof parent === 'undefined' ? 'undefined' : _typeof(parent)) !== 'object') { return false; } return true; }); if ((typeof parent === 'undefined' ? 'undefined' : _typeof(parent)) !== 'object') { return undefined; } var value = parent[pathArr[pathArr.length - 1]]; return (typeof value === 'undefined' ? 'undefined' : _typeof(value)) !== 'object' ? value : JSON.parse(JSON.stringify(value)); } }, { key: 'set', value: function set(path, value) { var pathArr = path.split('.'); var parent = this.model; pathArr.slice(0, -1).forEach(function (key) { if (parent[key] === undefined) { parent[key] = {}; } parent = parent[key]; }); parent[pathArr[pathArr.length - 1]] = (typeof value === 'undefined' ? 'undefined' : _typeof(value)) !== 'object' ? value : JSON.parse(JSON.stringify(value)); } }, { key: 'remove', value: function remove(path) { var pathArr = path.split('.'); var parent = this.model; pathArr.slice(0, -1).every(function (key) { parent = parent[key]; if ((typeof parent === 'undefined' ? 'undefined' : _typeof(parent)) !== 'object') { return false; } return true; }); if ((typeof parent === 'undefined' ? 'undefined' : _typeof(parent)) === 'object') { parent[pathArr[pathArr.length - 1]] = undefined; } } }, { key: 'has', value: function has(path) { var pathArr = path.split('.'); var parent = this.model; pathArr.slice(0, -1).every(function (key) { parent = parent[key]; if ((typeof parent === 'undefined' ? 'undefined' : _typeof(parent)) !== 'object') { return false; } return true; }); if ((typeof parent === 'undefined' ? 'undefined' : _typeof(parent)) !== 'object') { return false; } return parent[pathArr[pathArr.length - 1]] !== undefined; } }, { key: 'getAll', value: function getAll() { return JSON.parse(JSON.stringify(this.model)); } }, { key: 'clear', value: function clear() { this.model = {}; } }]); return Model; }(); /* route demo const routes = [{ path: 'market.list', get: query => fetch.get('/market/list').query(query).json() }, { path: 'market.setBrand' call: query => fetch.post('/market/setBrand').send(query).json() }]; */ var HttpModel = function (_Model) { _inherits(HttpModel, _Model); function HttpModel(routes) { _classCallCheck(this, HttpModel); if (!routes || !Array.isArray(routes)) { throw new Error('HttpModel should have routes(array) parameter'); } var _this = _possibleConstructorReturn(this, (HttpModel.__proto__ || Object.getPrototypeOf(HttpModel)).call(this, {})); _this.route = {}; routes.forEach(function (route) { var path = route.path; _this.route[path] = route; }); return _this; } _createClass(HttpModel, [{ key: 'get', value: function get(path, query) { var _this2 = this; var symbol = path + '.' + JSON.stringify(query); if (!_get(HttpModel.prototype.__proto__ || Object.getPrototypeOf(HttpModel.prototype), 'has', this).call(this, symbol)) { var getFunc = this.route[path] && this.route[path].get; if (!getFunc) { throw new Error(path + ' get route is not found'); } return getFunc(query).then(function (value) { _get(HttpModel.prototype.__proto__ || Object.getPrototypeOf(HttpModel.prototype), 'set', _this2).call(_this2, symbol, value); return _get(HttpModel.prototype.__proto__ || Object.getPrototypeOf(HttpModel.prototype), 'get', _this2).call(_this2, symbol); }); } return Promise.resolve(_get(HttpModel.prototype.__proto__ || Object.getPrototypeOf(HttpModel.prototype), 'get', this).call(this, symbol)); } }, { key: 'call', value: function call(path, query) { var callFunc = this.route[path] && this.route[path].call; if (!callFunc) { throw new Error(path + ' call route is not found'); } return callFunc(query); } }, { key: 'remove', value: function remove(path, query) { var symbol = path + '.' + JSON.stringify(query); _get(HttpModel.prototype.__proto__ || Object.getPrototypeOf(HttpModel.prototype), 'remove', this).call(this, symbol); } }]); return HttpModel; }(Model); exports.Model = Model; exports.HttpModel = HttpModel;