Commit 0c771b34 by Moxun1639

同一界面内,多CRUD共存

parent ca877166
...@@ -8,9 +8,11 @@ import Vue from 'vue' ...@@ -8,9 +8,11 @@ import Vue from 'vue'
* @param {*} options <br> * @param {*} options <br>
* @return crud instance. * @return crud instance.
* @example * @example
* 要使用多crud时,请在关联crud的组件处使用crud-tag进行标记,如:<jobForm :job-status="dict.job_status" crud-tag="job" />
*/ */
function CRUD(options) { function CRUD(options) {
const defaultOptions = { const defaultOptions = {
tag: '',
// 标题 // 标题
title: '', title: '',
// 请求数据的url // 请求数据的url
...@@ -530,6 +532,7 @@ function callVmHook(crud, hook) { ...@@ -530,6 +532,7 @@ function callVmHook(crud, hook) {
if (crud.debug) { if (crud.debug) {
console.log('callVmHook: ' + hook) console.log('callVmHook: ' + hook)
} }
const tagHook = crud.tag ? hook + '$' + crud.tag : null
let ret = true let ret = true
const nargs = [crud] const nargs = [crud]
for (let i = 2; i < arguments.length; ++i) { for (let i = 2; i < arguments.length; ++i) {
...@@ -542,6 +545,9 @@ function callVmHook(crud, hook) { ...@@ -542,6 +545,9 @@ function callVmHook(crud, hook) {
if (vm[hook]) { if (vm[hook]) {
ret = vm[hook].apply(vm, nargs) !== false && ret ret = vm[hook].apply(vm, nargs) !== false && ret
} }
if (tagHook && vm[tagHook]) {
ret = vm[tagHook].apply(vm, nargs) !== false && ret
}
}) })
return ret return ret
} }
...@@ -558,20 +564,46 @@ function mergeOptions(src, opts) { ...@@ -558,20 +564,46 @@ function mergeOptions(src, opts) {
return optsRet return optsRet
} }
function getCrudPiName(crudTag) {
return 'crud' + (crudTag ? '$' + crudTag : '')
}
/**
* 根据tag修正注入crud时from值
* @param {*} vm
*/
function reviseInject(vm) {
if (vm.$attrs['crud-tag']) {
const inject = vm.$options.inject
for (const k in inject) {
const v = inject[k]
const from = v.from
if (from === 'crud' || from.startsWith('crud.')) {
v.from = from.replace('crud', getCrudPiName(vm.$attrs['crud-tag']))
}
}
}
}
/** /**
* crud主页 * crud主页
*/ */
function presenter(crud) { function presenter(crud) {
const crudPiName = getCrudPiName(crud.tag)
return { return {
inject: ['crud'], inject: {
crud: {
from: crudPiName
}
},
beforeCreate() { beforeCreate() {
// 由于initInjections在initProvide之前执行,如果该组件自己就需要crud,需要在initInjections前准备好crud // 由于initInjections在initProvide之前执行,如果该组件自己就需要crud,需要在initInjections前准备好crud
this._provided = { this._provided = Object.assign(this._provided || {}, {
crud, [crudPiName]: crud,
'crud.query': crud.query, [crudPiName + '.query']: crud.query,
'crud.page': crud.page, [crudPiName + '.page']: crud.page,
'crud.form': crud.form [crudPiName + '.form']: crud.form
} })
}, },
data() { data() {
return { return {
...@@ -623,6 +655,9 @@ function header() { ...@@ -623,6 +655,9 @@ function header() {
from: 'crud.query' from: 'crud.query'
} }
}, },
beforeCreate() {
reviseInject(this)
},
created() { created() {
this.crud.registerVM('header', this, 1) this.crud.registerVM('header', this, 1)
}, },
...@@ -645,6 +680,9 @@ function pagination() { ...@@ -645,6 +680,9 @@ function pagination() {
from: 'crud.page' from: 'crud.page'
} }
}, },
beforeCreate() {
reviseInject(this)
},
created() { created() {
this.crud.registerVM('pagination', this, 2) this.crud.registerVM('pagination', this, 2)
}, },
...@@ -667,6 +705,9 @@ function form(defaultForm) { ...@@ -667,6 +705,9 @@ function form(defaultForm) {
from: 'crud.form' from: 'crud.form'
} }
}, },
beforeCreate() {
reviseInject(this)
},
created() { created() {
this.crud.registerVM('form', this, 3) this.crud.registerVM('form', this, 3)
this.crud.defaultForm = defaultForm this.crud.defaultForm = defaultForm
...@@ -692,6 +733,9 @@ function crud(options = {}) { ...@@ -692,6 +733,9 @@ function crud(options = {}) {
from: 'crud' from: 'crud'
} }
}, },
beforeCreate() {
reviseInject(this)
},
created() { created() {
this.crud.registerVM(options.type, this) this.crud.registerVM(options.type, this)
}, },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment