wiz-frameworks
Version:
wizlong react framework
101 lines (91 loc) • 4.25 kB
JavaScript
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
/**
* 模型数据处理类
* @author sin
* @date 2018-03-24
*/
import { utils_tool } from '../../tool';
import NProgress from 'nprogress';
var getType = utils_tool.getType,
log = utils_tool.log,
isType = utils_tool.isType;
export var modelUtils = {
/**
* 获取模型数据实体。当还设值时,获取默认的实体数据。
* @param {Map} modelMap 模型数据Map实体
* @param {string} key 数据获取key
*/
getModelEntity: function getModelEntity(modelMap) {
var key = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
return modelMap.get(key) ? modelMap.get(key) : modelMap.get('@default');
},
/**
* 获取数据模型loading状态
* @param {Object} state
* @param {String} key
*/
getLoadingState: function getLoadingState(state, key) {
var keyType = getType(key);
var currHref = '';
var href = window.location.href; // 浏览器地址栏中地址
if (!isType(state, 'Object')) {
log.error('getLoadingState: state 需要为Object类型');
return false;
} else if (keyType !== 'String' && keyType !== 'Array') {
log.error('getLoadingState: key 需要为 String 或 Array 类型');
return false;
} else {
if (keyType === 'Array') {
var r = {};
key.forEach(function (k) {
var objKey = k + 'Loading';
var isLoading = false;
NProgress.done(); // 页面请求完毕时调用 done 方法
currHref = href; // 将新页面的 href 值赋值给 currHref
if (state['loading'] && state['loading']['models'] && state['loading']['models'][k]) {
if (currHref !== href) {
// currHref 和 href 不一致时说明进行了页面跳转
NProgress.start(); // 页面开始加载时调用 start 方法
}
isLoading = true;
}
_extends(r, _defineProperty({}, objKey, isLoading));
});
return r;
} else {
var objKey = key + 'Loading';
var isLoading = false;
NProgress.done(); // 页面请求完毕时调用 done 方法
currHref = href; // 将新页面的 href 值赋值给 currHref
if (state['loading'] && state['loading']['models'] && state['loading']['models'][key]) {
var _href = window.location.href; // 浏览器地址栏中地址
if (currHref !== _href) {
// currHref 和 href 不一致时说明进行了页面跳转
NProgress.start(); // 页面开始加载时调用 start 方法
}
isLoading = true;
}
return _defineProperty({}, objKey, isLoading);
}
}
},
/**
* 将普通实体数据,转换成框架能识别的模型数据
* @param {object} 原始模型数据
* @param {object} 需要转换的模型数据
*/
convertModelEntity: function convertModelEntity(modelEntity, convertEntity) {
var result = _extends({}, convertEntity);
if (modelEntity['@namespace']) {
result['@namespace'] = modelEntity['@namespace'];
}
if (modelEntity['@payloadKey']) {
result['@payloadKey'] = modelEntity['@payloadKey'];
}
if (modelEntity['@default']) {
result['@default'] = modelEntity['@default'];
}
return result;
}
};