1
- import { Component , ViewChild } from '@angular/core' ;
2
- import { Http , Response } from '@angular/http' ;
1
+ import { Component , OnInit , ViewChild } from '@angular/core' ;
2
+ import { Http } from '@angular/http' ;
3
3
import { DataSource } from '@angular/cdk/table' ;
4
4
import { MdPaginator , MdSort } from '@angular/material' ;
5
5
import { Observable } from 'rxjs/Observable' ;
6
- import 'rxjs/add/operator/first' ;
7
- import 'rxjs/add/operator/startWith' ;
8
- import 'rxjs/add/operator/catch' ;
9
- import 'rxjs/add/operator/switchMap' ;
10
6
import 'rxjs/add/observable/merge' ;
11
7
import 'rxjs/add/observable/of' ;
12
- import 'rxjs/add/observable/interval ' ;
8
+ import 'rxjs/add/operator/catch ' ;
13
9
import 'rxjs/add/operator/map' ;
10
+ import 'rxjs/add/operator/startWith' ;
11
+ import 'rxjs/add/operator/switchMap' ;
14
12
15
13
@Component ( {
16
14
selector : 'table-http-example' ,
17
15
styleUrls : [ 'table-http-example.css' ] ,
18
16
templateUrl : 'table-http-example.html' ,
19
17
} )
20
- export class TableHttpExample {
21
- displayedColumns = [ 'created ' , 'state' , 'number' , 'title' ] ;
18
+ export class TableHttpExample implements OnInit {
19
+ displayedColumns = [ 'created_at ' , 'state' , 'number' , 'title' ] ;
22
20
exampleDatabase : ExampleHttpDao | null ;
23
21
dataSource : ExampleDataSource | null ;
24
22
25
23
@ViewChild ( MdPaginator ) paginator : MdPaginator ;
26
24
@ViewChild ( MdSort ) sort : MdSort ;
27
25
28
- constructor ( http : Http ) {
29
- this . exampleDatabase = new ExampleHttpDao ( http ) ;
30
- }
26
+ constructor ( private http : Http ) { }
31
27
32
28
ngOnInit ( ) {
33
- this . dataSource = new ExampleDataSource ( this . exampleDatabase ! ,
34
- this . sort , this . paginator ) ;
29
+ this . exampleDatabase = new ExampleHttpDao ( this . http ) ;
30
+ this . dataSource = new ExampleDataSource (
31
+ this . exampleDatabase ! , this . paginator , this . sort ) ;
35
32
}
36
33
}
37
34
35
+ export interface GithubApi {
36
+ items : GithubIssue [ ] ;
37
+ total_count : number ;
38
+ }
39
+
38
40
export interface GithubIssue {
41
+ created_at : string ;
39
42
number : string ;
40
43
state : string ;
41
44
title : string ;
42
- created : Date ;
43
45
}
44
46
45
47
/** An example database that the data source uses to retrieve data for the table. */
46
48
export class ExampleHttpDao {
47
49
constructor ( private http : Http ) { }
48
50
49
- getRepoIssues ( sort : string , order : string , page : number ) : Observable < Response > {
51
+ getRepoIssues ( sort : string , order : string , page : number ) : Observable < GithubApi > {
50
52
const href = 'https://api.github.com/search/issues' ;
51
53
const requestUrl =
52
- `${ href } ?q=repo:angular/material2&sort=${ sort } &order=${ order } &page=${ page + 1 } ` ;
53
- return this . http . get ( requestUrl ) ;
54
+ `${ href } ?q=repo:angular/material2&sort=${ sort } &order=${ order } &page=${ page + 1 } ` ;
55
+
56
+ return this . http . get ( requestUrl )
57
+ . map ( response => response . json ( ) as GithubApi ) ;
54
58
}
55
59
}
56
60
@@ -63,67 +67,48 @@ export class ExampleHttpDao {
63
67
*/
64
68
export class ExampleDataSource extends DataSource < GithubIssue > {
65
69
// The number of issues returned by github matching the query.
66
- resultsLength : number = 0 ;
70
+ resultsLength = 0 ;
67
71
isLoadingResults : boolean ;
68
72
isRateLimitReached : boolean ;
69
73
70
- constructor ( private _exampleDatabase : ExampleHttpDao ,
71
- private _sort : MdSort ,
72
- private _paginator : MdPaginator ) {
74
+ constructor ( private exampleDatabase : ExampleHttpDao ,
75
+ private paginator : MdPaginator ,
76
+ private sort : MdSort ) {
73
77
super ( ) ;
74
78
}
75
79
76
80
/** Connect function called by the table to retrieve one stream containing the data to render. */
77
81
connect ( ) : Observable < GithubIssue [ ] > {
78
82
const displayDataChanges = [
79
- this . _sort . mdSortChange ,
80
- this . _paginator . page ,
83
+ this . sort . mdSortChange ,
84
+ this . paginator . page
81
85
] ;
82
86
83
87
// If the user changes the sort order, reset back to the first page.
84
- this . _sort . mdSortChange . subscribe ( ( ) => {
85
- this . _paginator . pageIndex = 0 ;
86
- } ) ;
88
+ this . sort . mdSortChange . subscribe ( ( ) => this . paginator . pageIndex = 0 ) ;
87
89
88
90
return Observable . merge ( ...displayDataChanges )
89
- . startWith ( null )
90
- . switchMap ( ( ) => {
91
- this . isLoadingResults = true ;
92
- return this . _exampleDatabase . getRepoIssues (
93
- this . _sort . active , this . _sort . direction , this . _paginator . pageIndex ) ;
94
- } )
95
- . catch ( ( ) => {
96
- // Catch if the GitHub API has reached its rate limit. Return empty result.
97
- this . isRateLimitReached = true ;
98
- return Observable . of ( null ) ;
99
- } )
100
- . map ( result => {
101
- // Flip flag to show that loading has finished.
102
- this . isLoadingResults = false ;
103
- return result ;
104
- } )
105
- . map ( result => {
106
- if ( ! result ) { return [ ] ; }
107
-
108
- this . isRateLimitReached = false ;
109
- this . resultsLength = result . json ( ) . total_count ;
110
-
111
- return this . readGithubResult ( result ) ;
112
- } ) ;
113
-
114
-
91
+ . startWith ( null )
92
+ . switchMap ( ( ) => {
93
+ this . isLoadingResults = true ;
94
+ return this . exampleDatabase . getRepoIssues (
95
+ this . sort . active , this . sort . direction , this . paginator . pageIndex ) ;
96
+ } )
97
+ . map ( data => {
98
+ // Flip flag to show that loading has finished.
99
+ this . isLoadingResults = false ;
100
+ this . isRateLimitReached = false ;
101
+ this . resultsLength = data . total_count ;
102
+
103
+ return data . items ;
104
+ } )
105
+ . catch ( ( ) => {
106
+ this . isLoadingResults = false ;
107
+ // Catch if the GitHub API has reached its rate limit. Return empty data.
108
+ this . isRateLimitReached = true ;
109
+ return Observable . of ( null ) ;
110
+ } ) ;
115
111
}
116
112
117
113
disconnect ( ) { }
118
-
119
- private readGithubResult ( result : Response ) : GithubIssue [ ] {
120
- return result . json ( ) . items . map ( issue => {
121
- return {
122
- number : issue . number ,
123
- created : new Date ( issue . created_at ) ,
124
- state : issue . state ,
125
- title : issue . title ,
126
- } ;
127
- } ) ;
128
- }
129
114
}
0 commit comments