Skip to content

Commit

Permalink
model mixins, views
Browse files Browse the repository at this point in the history
  • Loading branch information
szuprefix committed Nov 20, 2019
1 parent 8a8cdf0 commit adab292
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 35 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-django",
"version": "0.6.0",
"version": "0.6.1",
"description": "个人实验项目, 本框架的目标是借鉴并超越django admin的自动化思想, 实现UI前端的极简快速定制开发",
"main": "index.js",
"files": [
Expand Down
22 changes: 21 additions & 1 deletion src/components/model/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import {Register} from '../../utils/app_model'
import axios from '../../configs/axios'
// import store from '../store'
export function joinErrors (errors) {
export function joinErrors(errors) {
let es = {}
for (let n in errors) {
es[n] = errors[n].join('')
Expand All @@ -24,6 +24,7 @@ export default function (appModel, defaults, eventor) {
options: {},
errors: {},
data: {},
viewsConfig: undefined,

init(){
this.config = Register.getConfig(this.appModel)
Expand Down Expand Up @@ -131,6 +132,25 @@ export default function (appModel, defaults, eventor) {
},
title () {
return !this.id && `新增${this.config.verbose_name}` || this.data['__str__']
},
loadViewsConfig () {
if (this.viewsConfig) {
return Promise.resolve(this.viewConfig)
}
return import(`@/views${this.getListUrl()}config.js`).then(m => {
return m.default || {}
}).catch((err) => {
console.error(err)
return {}
}).then(config => {
this.viewsConfig = config
return config
})
},
loadOptionsAndViewsConfig () {
return axios.all([this.loadOptions(), this.loadViewsConfig()]).then(axios.spread((restOptions, config) => {
return [restOptions, config]
}))
}
}
m.init()
Expand Down
33 changes: 22 additions & 11 deletions src/components/model/MultiCreator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
type: String,
default: ''
},
fields: Array,
items: Array,
appModel: String,
defaults: {
type: Object, default: () => {
Expand All @@ -74,15 +74,19 @@
splitChar: '\t',
began: false,
queueIndex: 0,
tmap: {}
tmap: {},
fieldItems: []
}
},
components: {
// PaperView
},
mounted (){
this.currentValue = this.value
this.model.loadOptions().then(this.loadForeignKeyTranslater).then(this.genRecords)
this.model.loadOptionsAndViewsConfig()
.then(this.normalizeItems)
.then(this.loadForeignKeyTranslater)
.then(this.genRecords)
},
methods: {
loadForeignKeyTranslater(){
Expand Down Expand Up @@ -122,6 +126,14 @@
if (value === this.currentValue) return
this.currentValue = value
},
normalizeItems() {
this.fieldItems = (this.items || this.viewConfig.items).map((a) => {
if (typeof a == 'string') {
return this.model.fieldConfigs[a]
}
return a
})
},
onEdit () {
this.edit = true
this.view = !this.view
Expand Down Expand Up @@ -158,11 +170,12 @@
},
postOne(){
let qi = this.queueIndex
let pkn = this.pk || this.viewConfig.pk
if (qi < this.records.length) {
let a = Object.assign({}, this.defaults, this.records[qi])
this.translateForeignKey(a)
let q = {}
q[this.pk] = a[this.pk]
q[pkn] = a[pkn]
this.$http.get(`${this.model.getListUrl()}?${Qs.stringify(q)}`).then(({data}) => {
if (data.count > 0) {
let r = data.results[0]
Expand Down Expand Up @@ -204,13 +217,8 @@
contentSample(){
return this.fieldNames.join(this.splitChar)
},
fieldItems(){
return this.fields.map((a) => {
if (typeof a == 'string') {
return this.model.fieldConfigs[a]
}
return a
})
viewConfig () {
return this.model.viewsConfig.batch
}
},
watch: {
Expand All @@ -221,6 +229,9 @@
this.$emit('change', this.currentValue)
this.genRecords()
},
items (val) {
this.normalizeItems()
},
splitChar () {
this.genRecords()
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/model/Table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
this.model.loadOptions().then((data) => {
let search = this.model.options.actions.SEARCH
this.$refs.search.init()
this.orderingFields = search.ordering_fields
// this.orderingFields = search.ordering_fields
return this.normalizeItems()
}).then(() => {
this.parentQueries = Object.assign({}, this.getParentQueries())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* Created by denishuang on 2019/9/3.
*/

import ModelForm from './Form.vue'
import ModelRelations from './Relations.vue'
import server_response from '../../mixins/server_response'
import ModelForm from '../../components/model/Form.vue'
import ModelRelations from '../../components/model/Relations.vue'
import server_response from '../server_response'
export default {
props: {
tab: Object,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Created by denishuang on 2019/9/3.
*/
import ModelTable from './Table.vue'
import server_response from '../../mixins/server_response'
import ModelTable from '../../components/model/Table.vue'
import server_response from '../server_response'
export default {
props: {
tab: Object
Expand Down
18 changes: 18 additions & 0 deletions src/mixins/model/multi_create_mixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Created by denishuang on 2019/9/3.
*/
import MultiCreator from '../../components/model/MultiCreator.vue'
import server_response from 'vue-django/src/mixins/server_response'
export default{
mixins: [server_response],
data(){
return {
records:"",
fields:['name']
}
},
components:{
MultiCreator
}

}
21 changes: 7 additions & 14 deletions src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,14 @@ let router = new Router({
routes: constRoutes
})

function import_edit(path){
function import_or_use_template(path, template){
return () => {
return import('@/views/' + path + '.vue').catch(() => {
return import('../components/model/EditPage.vue')
return import(`../views/model/${template}.vue`)
})
}
}

function import_list(path){
return () => {
return import('@/views/' + path + '.vue').catch(() => {
return import('../components/model/ListPage.vue')
})
}
}
export default router

export var genModelRouters = function (apps, importFunc, defaultLayout) {
Expand Down Expand Up @@ -74,7 +67,7 @@ export var genModelRouters = function (apps, importFunc, defaultLayout) {
let model = models[m]
let mname = model.verbose_name
let actions = model.actions || []
let itemActions = model.item_actions || []
let itemActions = model.itemActions || []
children.push({
path: `/${a}/${m}/`,
name: `${a}-${m}-list`,
Expand All @@ -84,7 +77,7 @@ export var genModelRouters = function (apps, importFunc, defaultLayout) {
icon: model.icon,
permission: ['*']
},
component: import_list(`${a}/${m}/list`)
component: import_or_use_template(`${a}/${m}/list`, 'list')
})
actions.forEach((action) => {
// console.log(`/${a}/${m}/${action.name}/`)
Expand All @@ -97,7 +90,7 @@ export var genModelRouters = function (apps, importFunc, defaultLayout) {
icon: model.icon,
permissions: action.permission || []
},
component: _import(`${a}/${m}/${action.name}`)
component: import_or_use_template(`${a}/${m}/${action.name}`, action.name)
})
})
children.push({
Expand All @@ -109,7 +102,7 @@ export var genModelRouters = function (apps, importFunc, defaultLayout) {
icon: model.icon,
permissions: ['change', 'add']
},
component: import_edit(`${a}/${m}/edit`)
component: import_or_use_template(`${a}/${m}/edit`, 'edit')
})
itemActions.forEach((action) => {
children.push({
Expand All @@ -121,7 +114,7 @@ export var genModelRouters = function (apps, importFunc, defaultLayout) {
icon: model.icon,
permissions: action.permission || []
},
component: _import(`${a}/${m}/${action.name}`)
component: import_or_use_template(`${a}/${m}/${action.name}`, action.name)
})
})
})
Expand Down
19 changes: 19 additions & 0 deletions src/views/model/batch.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<template>
<multi-creator :appModel="appModel">
</multi-creator>
</template>
<script>
import multi_create_mixin from 'vue-django/src/mixins/model/multi_create_mixin'
export default{
mixins: [multi_create_mixin],
data () {
return {
appModel: ''
}
},
created () {
let ps = this.$route.path.split('/')
this.appModel = `${ps[1]}.${ps[2]}`
}
}
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</model-form>
</template>
<script>
import edit_mixin from 'vue-django/src/components/model/edit_mixin'
import edit_mixin from 'vue-django/src/mixins/model/edit_mixin'
export default{
mixins: [edit_mixin],
data () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<model-table :appModel="appModel" v-model="data" ref="table"></model-table>
</template>
<script>
import list_mixin from 'vue-django/src/components/model/list_mixin'
import list_mixin from 'vue-django/src/mixins/model/list_mixin'
export default{
mixins: [list_mixin],
data () {
Expand Down

0 comments on commit adab292

Please sign in to comment.