|
|
@@ -91,6 +91,12 @@ export default {
|
|
|
},
|
|
|
paginationConfig () {
|
|
|
return this.schema.pagination
|
|
|
+ },
|
|
|
+ autoRefreshEachPage () {
|
|
|
+ return this.schema.autoRefreshEachPage
|
|
|
+ },
|
|
|
+ autoRefresh () {
|
|
|
+ return this.schema.autoRefresh || this.autoRefreshEachPage
|
|
|
}
|
|
|
},
|
|
|
watch: {
|
|
|
@@ -118,6 +124,18 @@ export default {
|
|
|
immediate: true
|
|
|
}
|
|
|
},
|
|
|
+ beforeCreate () {
|
|
|
+ this.$refreshTimer = -1
|
|
|
+ },
|
|
|
+ beforeDestroy () {
|
|
|
+ if (this.options) {
|
|
|
+ this.options.death = true
|
|
|
+ }
|
|
|
+ clearTimeout(this.$refreshTimer)
|
|
|
+ },
|
|
|
+ deactivated () {
|
|
|
+ clearTimeout(this.$refreshTimer)
|
|
|
+ },
|
|
|
activated () {
|
|
|
if (this.schema.keepalive) {
|
|
|
this.onPagination()
|
|
|
@@ -150,8 +168,7 @@ export default {
|
|
|
const options = this.options
|
|
|
if (!options.loading) {
|
|
|
options.loading = true
|
|
|
- const { list, transform } = this.schema
|
|
|
- list(options.params).then(
|
|
|
+ this.onFetchList(options).then(
|
|
|
({ data, totalCount }) => {
|
|
|
options.loading = false
|
|
|
options.error = false
|
|
|
@@ -166,6 +183,7 @@ export default {
|
|
|
return void this.pageTo(totalCount ? Math.ceil(totalCount / pageSize) : 1)
|
|
|
}
|
|
|
}
|
|
|
+ const { transform } = this.schema
|
|
|
options.list = transform ? data.map(transform) : data
|
|
|
},
|
|
|
() => {
|
|
|
@@ -179,6 +197,47 @@ export default {
|
|
|
})
|
|
|
}
|
|
|
},
|
|
|
+ onFetchList (options) {
|
|
|
+ clearTimeout(this.$refreshTimer)
|
|
|
+ const { pageNum } = options.params
|
|
|
+ return this.schema.list(options.params).then(data => {
|
|
|
+ if (this.autoRefresh && !options.death && (this.autoRefreshEachPage || pageNum === 1)) {
|
|
|
+ this.$refreshTimer = setTimeout(this.refreshCurrentPageOnBackground, 5000)
|
|
|
+ }
|
|
|
+ return data
|
|
|
+ })
|
|
|
+ },
|
|
|
+ refreshCurrentPageOnBackground () {
|
|
|
+ const options = this.options
|
|
|
+ if (!options.loading) {
|
|
|
+ this.onFetchList(options).then(
|
|
|
+ ({ data, totalCount }) => {
|
|
|
+ if (!options.loading && !options.death) {
|
|
|
+ options.error = false
|
|
|
+ options.totalCount = totalCount || data.length
|
|
|
+ if (this.isNeedPagination && this.isManualPagination) {
|
|
|
+ const { pageNum, pageSize } = options.params
|
|
|
+ data = data.slice((pageNum - 1) * pageSize, pageNum * pageSize)
|
|
|
+ }
|
|
|
+ if (data.length === 0) {
|
|
|
+ const { pageNum, pageSize } = options.params
|
|
|
+ if (pageNum > 1) {
|
|
|
+ return void this.pageTo(totalCount ? Math.ceil(totalCount / pageSize) : 1)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ const { transform } = this.schema
|
|
|
+ options.list = transform ? data.map(transform) : data
|
|
|
+ this.$emit('changed', options.list)
|
|
|
+ }
|
|
|
+ },
|
|
|
+ ({ isCancel }) => {
|
|
|
+ if (!isCancel && !options.loading && !options.death) {
|
|
|
+ this.$refreshTimer = setTimeout(this.refreshCurrentPageOnBackground, 2000)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ )
|
|
|
+ }
|
|
|
+ },
|
|
|
onClickButton ({ on }) {
|
|
|
if (on) {
|
|
|
on(this.options.params, this)
|
|
|
@@ -253,30 +312,6 @@ export default {
|
|
|
return void this.pageTo(options.params.pageNum - 1)
|
|
|
}
|
|
|
this.pageTo()
|
|
|
- },
|
|
|
- refreshCurrentPageOnBackground () {
|
|
|
- const options = this.options
|
|
|
- if (!options.loading) {
|
|
|
- const { list, transform } = this.schema
|
|
|
- list(options.params).then(({ data, totalCount }) => {
|
|
|
- if (!options.loading && !options.death) {
|
|
|
- options.error = false
|
|
|
- options.totalCount = totalCount || data.length
|
|
|
- if (this.isNeedPagination && this.isManualPagination) {
|
|
|
- const { pageNum, pageSize } = options.params
|
|
|
- data = data.slice((pageNum - 1) * pageSize, pageNum * pageSize)
|
|
|
- }
|
|
|
- if (data.length === 0) {
|
|
|
- const { pageNum, pageSize } = options.params
|
|
|
- if (pageNum > 1) {
|
|
|
- return void this.pageTo(totalCount ? Math.ceil(totalCount / pageSize) : 1)
|
|
|
- }
|
|
|
- }
|
|
|
- options.list = transform ? data.map(transform) : data
|
|
|
- this.$emit('changed', options.list)
|
|
|
- }
|
|
|
- })
|
|
|
- }
|
|
|
}
|
|
|
}
|
|
|
}
|