Skip to content

Commit

Permalink
小整理代码
Browse files Browse the repository at this point in the history
  • Loading branch information
JakHuang committed Nov 30, 2020
1 parent 1e09946 commit 189980a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
1 change: 0 additions & 1 deletion src/components/generator/ruleTrigger.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/**
* 用于生成表单校验,指定正则规则的触发方式。
* 未在此处声明无触发方式的组件将不生成rule!!
Expand Down
2 changes: 1 addition & 1 deletion src/components/parser/Parser.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const layouts = {
<el-col span={config.span}>
<el-form-item label-width={labelWidth} prop={scheme.__vModel__}
label={config.showLabel ? config.label : ''}>
<render conf={scheme} {...{ on: listeners }} />
<render conf={scheme} on={listeners} />
</el-form-item>
</el-col>
)
Expand Down
14 changes: 14 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,17 @@ export function deepClone(obj) {

return result
}

const toStr = Function.prototype.call.bind(Object.prototype.toString)
export function isObjectObject(t) {
return toStr(t) === '[object Object]'
}
export function isObjectArray(t) {
return toStr(t) === '[object Array]'
}
export function isObjectNull(t) {
return toStr(t) === '[object Null]'
}
export function isObjectUnde(t) {
return toStr(t) === '[object Undefined]'
}
19 changes: 12 additions & 7 deletions src/views/index/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ import {
inputComponents, selectComponents, layoutComponents, formConf
} from '@/components/generator/config'
import {
exportDefault, beautifierConf, isNumberStr, titleCase, deepClone
exportDefault, beautifierConf, isNumberStr, titleCase, deepClone, isObjectObject
} from '@/utils/index'
import {
makeUpHtml, vueTemplate, vueScript, cssStyle
Expand Down Expand Up @@ -267,22 +267,27 @@ export default {
})
},
methods: {
setObjectValueByStringKeys(obj, strKeys, val) {
setObjectValueReduce(obj, strKeys, data) {
const arr = strKeys.split('.')
arr.reduce((pre, item, i) => {
if (arr.length === i + 1) {
pre[item] = val
} else if (Object.prototype.toString.call(pre[item]) !== '[Object Object]') {
pre[item] = data
} else if (!isObjectObject(pre[item])) {
pre[item] = {}
}
return pre[item]
}, obj)
},
setRespData(component, respData) {
setRespData(component, resp) {
const { dataPath, renderKey, dataConsumer } = component.__config__
if (!dataPath || !dataConsumer) return
const data = dataPath.split('.').reduce((pre, item) => pre[item], respData)
this.setObjectValueByStringKeys(component, dataConsumer, data)
const respData = dataPath.split('.').reduce((pre, item) => pre[item], resp)
// 将请求回来的数据,赋值到指定属性。
// 以el-tabel为例,根据Element文档,应该将数据赋值给el-tabel的data属性,所以dataConsumer的值应为'data';
// 此时赋值代码可写成 component[dataConsumer] = respData;
// 但为支持更深层级的赋值(如:dataConsumer的值为'options.data'),使用setObjectValueReduce
this.setObjectValueReduce(component, dataConsumer, respData)
const i = this.drawingList.findIndex(item => item.__config__.renderKey === renderKey)
if (i > -1) this.$set(this.drawingList, i, component)
},
Expand Down

0 comments on commit 189980a

Please sign in to comment.