You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.2 KiB
JavaScript

/*
* @Author: your name
* @Date: 2020-10-28 14:40:52
* @LastEditTime: 2020-12-29 14:33:05
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: \dbadmin\src\store\modules\query.js
*/
import { Module, VuexModule, Mutation } from 'vuex-module-decorators'
export const DEFAULT_TAG = 'default'
function nullObj() {
return Object.create(null)
}
@Module({ name: 'query', namespaced: true })
class Query extends VuexModule {
data = nullObj()
pageData = nullObj()
fetchs = nullObj()
@Mutation
init(data) {
let path = ''
let initData = nullObj()
let tag = DEFAULT_TAG
if (typeof data === 'string') {
path = data
} else if (typeof data === 'object') {
if (data.path) path = data.path
if (data.initData) initData = data.initData
if (data.tag) tag = data.tag
}
if (!path) throw new Error('path不能为空')
initPath.call(this, path)
if (!this.data[path][tag]) {
this.data[path][tag] = initData
}
if (!this.pageData[path][tag]) {
this.pageData[path][tag] = {
page: 1,
size: 20
}
}
}
@Mutation
setData({ path, data, tag = DEFAULT_TAG }) {
initPath.call(this, path)
this.data[path][tag] = data
}
@Mutation
registerFetch({ path, func, tag = DEFAULT_TAG }) {
initPath.call(this, path)
this.fetchs[path][tag] = func
}
@Mutation
unregisterFetch(path, tag = DEFAULT_TAG) {
initPath.call(this, path)
delete this.fetchs[path][tag]
}
}
function initPath(path) {
if (this instanceof Window) {
throw new Error('initPath this对象没有绑定')
}
if (!this.data[path]) this.data[path] = nullObj()
if (!this.pageData[path]) this.pageData[path] = nullObj()
if (!this.fetchs[path]) this.fetchs[path] = nullObj()
}
// function initTag(path, tag) {
// if (this instanceof Window) {
// throw new Error('initPath this对象没有绑定')
// }
// initTag.call(path)
// if (!this.data[path][tag]) this.data[path][tag] = nullObj()
// if (!this.pageData[path][tag]) this.pageData[path][tag] = nullObj()
// if (!this.fetchs[path][tag]) this.fetchs[path][tag] = nullObj()
// }
export default Query