UNPKG

react-zeanium-ui-ui

Version:

Zeanium UI Framework for React.js

167 lines (148 loc) 4.13 kB
var Http = window.Http; var Util = window.Util; var HOST = 'http://0.0.0.0:8080/'; function Request(url, data, method) { this._className = 'Request'; this._url = url; this._data = data; this._method = method; } Request.prototype.exec = function (url, data, method){ var _url = url || this._url, _data = data || this._data, _method = method || this._method||'GET'; return Http[_method.toLowerCase()](Store.fixURL(_url), _data); } Request.prototype.refresh = function (){ this.exec(); } function DataSource(data, argv) { this.reset(data, argv); } DataSource.prototype.exec = function (){ var _data = this._data; if(!_data){ return; } var _temp = this._argv.onSubmitBefore && this._argv.onSubmitBefore(_data); if(_temp===false){ return; } if(_temp!==undefined){ _data = _temp; } if(_data._className=="Request"){ _data.exec().then(function (data){ this._argv.onSuccess && this._argv.onSuccess(data); }.bind(this), function (data){ Popup.message({ content: 'Load Error, Message: ' + data.result + ', Status: ' + data.status, type: 'danger' }); this._argv.onError && this._argv.onError(data); }.bind(this)); } else { this._argv.onSuccess && this._argv.onSuccess(_data); } } DataSource.prototype.reset = function (data, argv){ this._data = data; this._argv = argv || this._argv || {}; if(this._argv.autoLoad){ this.exec(); } } DataSource.prototype.setArgv = function (argv){ if(this._data && this._data._className=="Request"){ Util.extend(this._data._data, argv||{}); this._data.refresh(); } } DataSource.prototype.refresh = function (){ this.exec(); } function APILoader(config) { this.reload(config); } APILoader.prototype.parse = function (prefix, config){ var _prefix = prefix || '', _key = null, _value = null; for(var key in config){ _key = _prefix + '/' + key; _value = config[key]; if(key.indexOf('$')!=-1){ this._vars[key.slice(1)] = _value; continue; } switch (typeof _value) { case 'string': this._apis[_key] = this.format(_value); break; case 'object': this.parse(_key, _value); break; } } } APILoader.prototype.format = function (path){ var _value = null; path = path.replace(/\$/g, ''); for(var _key in this._vars){ _value = this._vars[_key]; if(_value !== undefined){ path = path.replace(new RegExp('{' + _key + '}', 'gi'), _value); } } return _value = null, path; } APILoader.prototype.get = function (key){ return this._apis[key]; } APILoader.prototype.reload = function (config){ this._apis = {}; this._vars = {}; this.parse('', config); } module.exports = { init: function (url, data, method){ return new Request(url, data, method); }, post: function (url, data){ return new Request(url, data, "POST"); }, delete: function (url, data){ return new Request(url, data, "DELETE"); }, put: function (url, data){ return new Request(url, data, "PUT"); }, get: function (url, data){ return new Request(this.formatURL(url, data), data, "GET"); }, setHost: function (value){ HOST = value; }, fixApi: function (api){ if(this._apiLoader){ api = this._apiLoader.get(api) || api; } return api; }, fixURL: function (url) { if(url.indexOf('http://') === -1){ url = HOST + url; } return url; }, formatURL: function (url, data){ for(var key in data){ url = url.replace(new RegExp('{' + key + '}', 'gi'), data[key]||''); } return url; }, dataSource: function (data, argv) { return new DataSource(data, argv); }, loadApi: function (api){ this._apiLoader = new APILoader(api); } }