Skip to content

Commit

Permalink
perf: form传递方式改为provide/inject方式
Browse files Browse the repository at this point in the history
  • Loading branch information
greper committed Jul 22, 2020
1 parent 8f73e66 commit 158a225
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 27 deletions.
3 changes: 3 additions & 0 deletions packages/d2-crud-plus-example/src/business/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ Vue.use(D2pUploader, {
}).then(ret => {
return ret.data
})
},
sdkOpts: { // sdk配置
secure: true // 默认为非https上传,为了安全,设置为true
}
},
qiniu: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const crudOptions = (vm) => {
defaultSpan: 24 // 默认的表单 span
},
searchOptions: {
// debounce: false, //关闭防抖
valueChange: true // 搜索框开启valueChange
},
columns: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
</template>

<script>
import dict from '../../utils/util.dicts'
import formatDict from '../../mixins/format-dict'
// 级联数据格式化组件
export default {
name: 'cascade-format',
mixins: [formatDict],
props: {
// 值<br/>
// 单选时 '1,2,3' 或 [1,2,3]<br/>
Expand All @@ -45,14 +46,9 @@ export default {
},
data () {
return {
options: []
returnType: 'data'
}
},
mounted () {
dict.get(this.dict).then((data) => {
this.$set(this, 'options', data)
})
},
computed: {
labels () {
if (this.value == null) {
Expand Down Expand Up @@ -103,8 +99,8 @@ export default {
childrenName = this.dict.children
}
let labelArr = []
if (this.options != null) {
let dict = this.options
if (this.data != null) {
let dict = this.data
for (let value of arr) {
if (dict != null) {
let dictItem = this.getDictItem(value, dict)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default {
if (this.value == null || this.value === '') {
return []
}
let dictDataMap = this.dictDataMap
let dictDataMap = this[this.returnType]
let valueArr = []
let options = []
if (typeof (this.value) === 'string' && this.multiple && this.separator != null && this.separator !== '') {
Expand Down
8 changes: 7 additions & 1 deletion packages/d2-crud-plus/src/lib/components/search/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
<render-custom-component
v-else-if="item.component && item.component.name"
v-model="form[item.key]"
:_form="form"
:ref="'form_item_'+item.key"
:component-name="item.component.name"
:props="getComponentProps(item)"
Expand Down Expand Up @@ -69,6 +68,13 @@
import lodash from 'lodash'
export default {
name: 'crud-search',
provide: function () {
return {
d2CrudContext: {
getForm: this.getForm
}
}
},
props: {
// 查询参数,options.form为表单初始值
options: {
Expand Down
16 changes: 10 additions & 6 deletions packages/d2-crud-plus/src/lib/mixins/format-dict.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import dict from '../utils/util.dicts'

export default {
inject: ['d2CrudContext'],
props: {
// 数据字典配置
// {url:'xxx',data:[],value:'',label:'',children:''}
Expand All @@ -14,11 +15,10 @@ export default {
},
data () {
return {
dictDataMap: {}
dataMap: {},
data: [],
returnType: 'dataMap'
}
},
mounted () {

},
created () {
if (this.dict) {
Expand All @@ -29,8 +29,12 @@ export default {
},
methods: {
loadDict () {
dict.get(this.dict, { returnType: 'dataMap', component: this, form: this.$attrs._form }).then((dataMap) => {
this.$set(this, 'dictDataMap', dataMap)
const options = { component: this, returnType: this.returnType }
if (this.d2CrudContext) {
options.form = this.d2CrudContext.getForm()
}
dict.get(this.dict, options).then((dataMap) => {
this.$set(this, this.returnType, dataMap)
})
}
}
Expand Down
10 changes: 6 additions & 4 deletions packages/d2-crud-plus/src/lib/mixins/input-dict.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import dict from '../utils/util.dicts'

export default {
inject: ['d2CrudContext'],
props: {
// 数据字典配置
// {url:'xxx',data:[],value:'',label:'',children:''}
Expand Down Expand Up @@ -78,11 +79,12 @@ export default {
return this.dictOptions
},
loadDict () {
const options = {}
if (this.$attrs && this.$attrs._form) {
options.form = this.$attrs._form
const options = { component: this }
if (this.d2CrudContext) {
options.form = this.d2CrudContext.getForm()
}
dict.get(this.dict, { form: this.$attrs._form, component: this }).then((data) => {
console.log('context--------------', this.d2CrudContext, options)
dict.get(this.dict, options).then((data) => {
this.$set(this, 'dictOptions', data)
if (this.onReady != null) {
this.onReady(this)
Expand Down
1 change: 1 addition & 0 deletions packages/d2-crud-plus/src/lib/utils/util.dicts.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function get (dict, options) {
}
// 远程获取
let item = cacheKey != null ? cache.get(cacheKey) : null
console.log('11111111111111111', dict, item, cacheKey)
if (item == null || item.error === true) {
// 还没加载过
if (item == null) {
Expand Down
2 changes: 1 addition & 1 deletion packages/d2-crud-x/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
],
rules: {
'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off'
},
parserOptions: {
parser: 'babel-eslint'
Expand Down
11 changes: 10 additions & 1 deletion packages/d2-crud-x/src/components/d2-cell.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<render-custom-component
v-else-if="item.component && item.component.name"
v-model="row[item.key]"
:_form="row"
:component-name="item.component.name"
:disabled="getComponentProp(item,item.key,'disabled', false)"
:readonly="getComponentProp(item,item.key,'readonly', false)"
Expand Down Expand Up @@ -39,6 +38,13 @@ import renderCustomComponent from '../components/renderCustomComponent.vue'
import _get from 'lodash.get'
export default {
name: 'd2-cell',
provide: function () {
return {
d2CrudContext: {
getForm: this.getRow
}
}
},
mixins: [
utils
],
Expand All @@ -63,6 +69,9 @@ export default {
* @description lodash.get
*/
_get,
getRow () {
return this.row
},
handleCellDataChange (value, column) {
column.value = value
this.$emit('cell-data-change', column)
Expand Down
3 changes: 3 additions & 0 deletions packages/d2-crud-x/src/components/d2-column.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ export default {
}
},
methods: {
getRow () {
return this.scope.row
},
handleCellDataChange (column) {
this.$emit('cell-data-change', column)
},
Expand Down
1 change: 0 additions & 1 deletion packages/d2-crud-x/src/components/d2-form-item.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
<render-custom-component
v-else-if="getFormComponent().name"
v-model="formData[colKey]"
:_form="formData"
ref="targetWrapper"
:component-name="getFormComponent().name"
:disabled="getFormComponentAttr('disabled', false)"
Expand Down
12 changes: 10 additions & 2 deletions packages/d2-crud-x/src/d2-crud.vue
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@
@current-change="handlePaginationCurrentChange"
@prev-click="handlePaginationPrevClick"
@next-click="handlePaginationNextClick"
><slot name="PaginationPrefixSlot" />
>
<slot name="PaginationPrefixSlot" />
</el-pagination>
</div>
<slot name="footer"/>
Expand Down Expand Up @@ -222,7 +223,7 @@
<slot name="FormBodyAppendSlot" :mode="formMode" :form="formData"/>
</el-form>
<div slot="footer">
<slot name="FormFooterSlot" :mode="formMode" :data="formData" />
<slot name="FormFooterSlot" :mode="formMode" :data="formData" />
<el-button v-if="getAttribute(formOptions,'saveButtonShow', true)"
:size="getAttribute(formOptions,'saveButtonSize', null)"
:type="getAttribute(formOptions,'saveButtonType' ,null)"
Expand Down Expand Up @@ -255,6 +256,13 @@ import D2Button from './components/d2-button/component'
export default {
name: 'd2-crud',
provide: function () {
return {
d2CrudContext: {
getForm: this.getFormData
}
}
},
mixins: [
base,
data,
Expand Down
4 changes: 3 additions & 1 deletion packages/d2-crud-x/src/mixin/dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ export default {
computed: {
},
methods: {

getFormData () {
return this.formData
},
openDialog (index, row, templage) {
const formData = {}
this.formTemplateStorage = templage ? _clonedeep(templage) : {}
Expand Down

0 comments on commit 158a225

Please sign in to comment.