解决多crud,对inject修改造成注入混乱,弃用Vue注入方式
This commit is contained in:
@@ -12,7 +12,7 @@ import Vue from 'vue'
|
|||||||
*/
|
*/
|
||||||
function CRUD(options) {
|
function CRUD(options) {
|
||||||
const defaultOptions = {
|
const defaultOptions = {
|
||||||
tag: '',
|
tag: 'default',
|
||||||
// 标题
|
// 标题
|
||||||
title: '',
|
title: '',
|
||||||
// 请求数据的url
|
// 请求数据的url
|
||||||
@@ -36,7 +36,7 @@ function CRUD(options) {
|
|||||||
// CRUD Method
|
// CRUD Method
|
||||||
crudMethod: {
|
crudMethod: {
|
||||||
add: (form) => {},
|
add: (form) => {},
|
||||||
delete: (id) => {},
|
del: (id) => {},
|
||||||
edit: (form) => {},
|
edit: (form) => {},
|
||||||
get: (id) => {}
|
get: (id) => {}
|
||||||
},
|
},
|
||||||
@@ -564,46 +564,35 @@ function mergeOptions(src, opts) {
|
|||||||
return optsRet
|
return optsRet
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCrudPiName(crudTag) {
|
|
||||||
return 'crud' + (crudTag ? '$' + crudTag : '')
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据tag修正注入crud时from值
|
* 查找crud
|
||||||
* @param {*} vm
|
* @param {*} vm
|
||||||
|
* @param {string} tag
|
||||||
*/
|
*/
|
||||||
function reviseInject(vm) {
|
function lookupCrud(vm, tag) {
|
||||||
if (vm.$attrs['crud-tag']) {
|
tag = tag || vm.$attrs['crud-tag'] || 'default'
|
||||||
const inject = vm.$options.inject
|
// function lookupCrud(vm, tag) {
|
||||||
for (const k in inject) {
|
if (vm.$crud) {
|
||||||
const v = inject[k]
|
const ret = vm.$crud[tag]
|
||||||
const from = v.from
|
if (ret) {
|
||||||
if (from === 'crud' || from.startsWith('crud.')) {
|
return ret
|
||||||
v.from = from.replace('crud', getCrudPiName(vm.$attrs['crud-tag']))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return vm.$parent ? lookupCrud(vm.$parent, tag) : undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* crud主页
|
* crud主页
|
||||||
*/
|
*/
|
||||||
function presenter(crud) {
|
function presenter(crud) {
|
||||||
const crudPiName = getCrudPiName(crud.tag)
|
|
||||||
return {
|
return {
|
||||||
inject: {
|
|
||||||
crud: {
|
|
||||||
from: crudPiName
|
|
||||||
}
|
|
||||||
},
|
|
||||||
beforeCreate() {
|
beforeCreate() {
|
||||||
// 由于initInjections在initProvide之前执行,如果该组件自己就需要crud,需要在initInjections前准备好crud
|
this.$crud = this.$crud || {}
|
||||||
this._provided = Object.assign(this._provided || {}, {
|
if (this.$crud[crud.tag]) {
|
||||||
[crudPiName]: crud,
|
console.error('[CRUD error]: ' + 'crud with tag [' + crud.tag + ' is already exist')
|
||||||
[crudPiName + '.query']: crud.query,
|
}
|
||||||
[crudPiName + '.page']: crud.page,
|
this.$crud[crud.tag] = crud
|
||||||
[crudPiName + '.form']: crud.form
|
crud.registerVM('presenter', this, 0)
|
||||||
})
|
|
||||||
},
|
},
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
@@ -614,13 +603,12 @@ function presenter(crud) {
|
|||||||
parseTime
|
parseTime
|
||||||
},
|
},
|
||||||
created() {
|
created() {
|
||||||
this.crud.registerVM('presenter', this, 0)
|
|
||||||
if (crud.queryOnPresenterCreated) {
|
if (crud.queryOnPresenterCreated) {
|
||||||
crud.toQuery()
|
crud.toQuery()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
beforeDestroy() {
|
destroyed() {
|
||||||
this.crud.unregisterVM(this)
|
crud.unregisterVM(this)
|
||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
const columns = []
|
const columns = []
|
||||||
@@ -647,21 +635,12 @@ function presenter(crud) {
|
|||||||
*/
|
*/
|
||||||
function header() {
|
function header() {
|
||||||
return {
|
return {
|
||||||
inject: {
|
|
||||||
crud: {
|
|
||||||
from: 'crud'
|
|
||||||
},
|
|
||||||
query: {
|
|
||||||
from: 'crud.query'
|
|
||||||
}
|
|
||||||
},
|
|
||||||
beforeCreate() {
|
beforeCreate() {
|
||||||
reviseInject(this)
|
this.crud = lookupCrud(this)
|
||||||
},
|
this.query = this.crud.query
|
||||||
created() {
|
|
||||||
this.crud.registerVM('header', this, 1)
|
this.crud.registerVM('header', this, 1)
|
||||||
},
|
},
|
||||||
beforeDestroy() {
|
destroyed() {
|
||||||
this.crud.unregisterVM(this)
|
this.crud.unregisterVM(this)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -672,21 +651,12 @@ function header() {
|
|||||||
*/
|
*/
|
||||||
function pagination() {
|
function pagination() {
|
||||||
return {
|
return {
|
||||||
inject: {
|
|
||||||
crud: {
|
|
||||||
from: 'crud'
|
|
||||||
},
|
|
||||||
page: {
|
|
||||||
from: 'crud.page'
|
|
||||||
}
|
|
||||||
},
|
|
||||||
beforeCreate() {
|
beforeCreate() {
|
||||||
reviseInject(this)
|
this.crud = lookupCrud(this)
|
||||||
},
|
this.page = this.crud.page
|
||||||
created() {
|
|
||||||
this.crud.registerVM('pagination', this, 2)
|
this.crud.registerVM('pagination', this, 2)
|
||||||
},
|
},
|
||||||
beforeDestroy() {
|
destroyed() {
|
||||||
this.crud.unregisterVM(this)
|
this.crud.unregisterVM(this)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -697,23 +667,16 @@ function pagination() {
|
|||||||
*/
|
*/
|
||||||
function form(defaultForm) {
|
function form(defaultForm) {
|
||||||
return {
|
return {
|
||||||
inject: {
|
|
||||||
crud: {
|
|
||||||
from: 'crud'
|
|
||||||
},
|
|
||||||
form: {
|
|
||||||
from: 'crud.form'
|
|
||||||
}
|
|
||||||
},
|
|
||||||
beforeCreate() {
|
beforeCreate() {
|
||||||
reviseInject(this)
|
this.crud = lookupCrud(this)
|
||||||
|
this.form = this.crud.form
|
||||||
|
this.crud.registerVM('form', this, 3)
|
||||||
},
|
},
|
||||||
created() {
|
created() {
|
||||||
this.crud.registerVM('form', this, 3)
|
|
||||||
this.crud.defaultForm = defaultForm
|
this.crud.defaultForm = defaultForm
|
||||||
this.crud.resetForm()
|
this.crud.resetForm()
|
||||||
},
|
},
|
||||||
beforeDestroy() {
|
destroyed() {
|
||||||
this.crud.unregisterVM(this)
|
this.crud.unregisterVM(this)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -728,18 +691,11 @@ function crud(options = {}) {
|
|||||||
}
|
}
|
||||||
options = mergeOptions(defaultOptions, options)
|
options = mergeOptions(defaultOptions, options)
|
||||||
return {
|
return {
|
||||||
inject: {
|
|
||||||
crud: {
|
|
||||||
from: 'crud'
|
|
||||||
}
|
|
||||||
},
|
|
||||||
beforeCreate() {
|
beforeCreate() {
|
||||||
reviseInject(this)
|
this.crud = lookupCrud(this)
|
||||||
},
|
|
||||||
created() {
|
|
||||||
this.crud.registerVM(options.type, this)
|
this.crud.registerVM(options.type, this)
|
||||||
},
|
},
|
||||||
beforeDestroy() {
|
destroyed() {
|
||||||
this.crud.unregisterVM(this)
|
this.crud.unregisterVM(this)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user