Skip to content

Commit 3bb0be1

Browse files
Unreal TsaiUnreal Tsai
Unreal Tsai
authored and
Unreal Tsai
committed
fix PagingController
1 parent 60a14e7 commit 3bb0be1

8 files changed

+80
-37
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
### frontend view controllers

dist/list.controller.js

+7-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/list.controller.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/paging.controller.js

+25-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/paging.controller.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "ctrls",
33
"email": "[email protected]",
4-
"version": "0.0.1",
4+
"version": "0.0.3",
55
"description": "frontend view controllers",
66
"main": "dist/index.js",
77
"scripts": {
@@ -16,5 +16,6 @@
1616
"dependencies": {
1717
"@types/lodash": "^4.14.165",
1818
"lodash": "^4.17.20"
19-
}
19+
},
20+
"homepage": "https://github.com/CaiWenlie/ctrls"
2021
}

src/list.controller.ts

+9-12
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,17 @@ export default class ListController<T = any, P = any> {
3333
.finally(() => {
3434
this.loading = false
3535
})
36-
this.list = res.data
36+
this.list = this.resReader(res).list
3737
return res
3838
}
3939

40+
// to be customised
41+
resReader(res: any) {
42+
return {
43+
list: res.data
44+
}
45+
}
46+
4047
async fetchDetail(data: Partial<T>) {
4148
return this.service.detail!(data)
4249
}
@@ -73,19 +80,9 @@ export default class ListController<T = any, P = any> {
7380
// T: list item类型
7481
// P: list param类型
7582
export interface TListService<T, P> {
76-
list(params: P & any): Promise<Res<T[]>>
83+
list(params: P & any): Promise<any>
7784
create?(data: Partial<T> & any): Promise<any>
7885
remove?(data: Partial<T> & any): Promise<any>
7986
update?(data: Partial<T> & any): Promise<any>
8087
detail?(data: Partial<T> & any): Promise<any>
8188
}
82-
83-
export interface Res<T = any> {
84-
success: boolean
85-
data: T
86-
statusCode: string
87-
desc: string
88-
itemTotal?: number
89-
pageTotal?: number
90-
relation?: any
91-
}

src/paging.controller.ts

+33-14
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import ListController, { Res, TListService } from './list.controller'
1+
import ListController, { TListService } from './list.controller'
22

3-
type TPagingService<T, P> = Omit<TListService<T, P>, 'list'> & { list: (params: P & Partial<Paging>) => Promise<PagingRes<T[]>> }
3+
type TPagingService<T, P> = Omit<TListService<T, P>, 'list'> & { list: (params: P & Partial<Pagination>) => Promise<any> }
44

55
// 通用list翻页控制器
66
// T: list item类型
77
// P: list param类型
88
export default class PagingController<T = any, P = any> extends ListController<T, P> {
99
service: TPagingService<T, P>
10-
paging: Paging = {
10+
_pagination: Pagination = {
1111
itemTotal: 0,
1212
pageIndex: 1,
1313
pageSize: 10
@@ -18,10 +18,33 @@ export default class PagingController<T = any, P = any> extends ListController<T
1818
this.service = service
1919
}
2020

21-
async fetchList(data?: Partial<Paging> & Partial<P>) {
22-
const params = { ...this.params, ...this.paging, ...data }
21+
get pagination() {
22+
return this.paginationGetter(this._pagination)
23+
}
24+
25+
// to be customised
26+
paginationGetter(pagination: Pagination) {
27+
return {
28+
itemTotal: pagination.itemTotal,
29+
pageIndex: pagination.pageIndex,
30+
pageSize: pagination.pageSize
31+
}
32+
}
33+
34+
// to be customised
35+
resReader(res: any) {
36+
return {
37+
list: res.data,
38+
itemTotal: res.itemTotal,
39+
pageIndex: res.pageIndex,
40+
pageSize: res.pageSize
41+
}
42+
}
43+
44+
async fetchList(data?: Partial<Pagination> & Partial<P>) {
45+
const params = { ...this.params, ...this.pagination, ...data }
2346
Object.keys(params).forEach(key => {
24-
if (!['itemTotal', 'pageIndex', 'pageSize'].includes(key)) {
47+
if (!Object.keys(this.pagination).includes(key)) {
2548
(this.params as any)[key] = (params as any)[key]
2649
}
2750
})
@@ -30,19 +53,15 @@ export default class PagingController<T = any, P = any> extends ListController<T
3053
.finally(() => {
3154
this.loading = false
3255
})
33-
const { itemTotal, pageIndex } = res
34-
const { pageSize } = params
35-
this.paging = { itemTotal, pageIndex, pageSize }
36-
this.list = res.data
56+
const { list, itemTotal, pageIndex, pageSize } = this.resReader(res)
57+
this._pagination = { itemTotal, pageIndex, pageSize }
58+
this.list = list
3759
return res
3860
}
3961
}
4062

41-
export interface Paging {
63+
export interface Pagination {
4264
pageSize: number
4365
pageIndex: number
4466
itemTotal: number
4567
}
46-
47-
export type PagingParams = Partial<Paging>
48-
export type PagingRes<T = any> = Res<T> & Paging

0 commit comments

Comments
 (0)