Skip to content

Commit

Permalink
model relations
Browse files Browse the repository at this point in the history
  • Loading branch information
szuprefix committed May 29, 2020
1 parent b09c7ae commit 44ebe37
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
41 changes: 25 additions & 16 deletions src/components/mobile/model/Relations.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,57 @@
<tab v-model="tab" :animate="false">
<tab-item :active-class="`active-${i%3+1}`" v-for="a, i in items" :key="a.name">{{a.label}}</tab-item>
</tab>
<component :is="relationComponent" :owner="owner"
v-if="relationComponent"></component>
<component :is="tabItems[tab].component" :owner="owner" v-bind="[tabItems[tab]]"
v-if="loaded"></component>
</div>
</template>
<script>
import {Tab, TabItem} from 'vux'
import arrayNormalize from 'vue-django/src/utils/array_normalize'
export default{
props: {
current: Number,
items: Array,
owner: Object
},
data () {
return {
cache: this.$store.state.user.storage.newCache(`${this.owner.appModel}.n${this.owner.id}.relations.tab`),
tab: undefined,
modelListComponents: [],
relationComponent: undefined
tabItems: [],
loaded: false
}
},
components: {Tab, TabItem},
created () {
this.tab = this.cache.read() || 0
this.items.forEach((a, i) => {
this.loadModelListComponent(i)
})
this.normalizeItems()
this.tab = this.current || this.cache.read() || 0
this.load(this.tab)
},
methods: {
loadModelListComponent (i) {
let appModel = this.items[i].name
import(`@/views/${appModel.replace('.', '/')}/components/List.vue`).then(m => {
this.modelListComponents[i] = m.default || {}
if (i === this.tab) {
this.relationComponent = this.modelListComponents[this.tab]
}
normalizeItems () {
this.tabItems = arrayNormalize(this.items, {}, (a, i) => {
a.component = a.component || `${a.name.replace('.', '/')}/components/List`
return a
})
},
load (i) {
this.loaded = false
let a = this.tabItems[i]
let c = a.component
import(`@/views/${c}.vue`).then(m => {
this.tabItems[i] = {...a, component: m.default || {}}
this.loaded = true
})
}
},
computed: {},
watch: {
tab (v) {
this.relationComponent = this.modelListComponents[this.tab]
this.cache.save(v)
if (typeof this.tabItems[v].component === 'string') {
this.load(v)
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/array_normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

export default function (items, templates, normalize) {
return items.map((a) => {
return items.map((a, i) => {
let d = {}
if (typeof a === 'string' || a instanceof String) {
Object.assign(d, templates[a])
Expand All @@ -14,7 +14,7 @@ export default function (items, templates, normalize) {
Object.assign(d, templates[a.name], a)
}
if (normalize) {
d = normalize(d)
d = normalize(d, i)
}
return d
})
Expand Down

0 comments on commit 44ebe37

Please sign in to comment.