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' ;
33import { DataSource } from '@angular/cdk/table' ;
44import { MdPaginator , MdSort } from '@angular/material' ;
55import { 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' ;
106import 'rxjs/add/observable/merge' ;
117import 'rxjs/add/observable/of' ;
12- import 'rxjs/add/observable/interval ' ;
8+ import 'rxjs/add/operator/catch ' ;
139import 'rxjs/add/operator/map' ;
10+ import 'rxjs/add/operator/startWith' ;
11+ import 'rxjs/add/operator/switchMap' ;
1412
1513@Component ( {
1614 selector : 'table-http-example' ,
1715 styleUrls : [ 'table-http-example.css' ] ,
1816 templateUrl : 'table-http-example.html' ,
1917} )
20- export class TableHttpExample {
21- displayedColumns = [ 'created ' , 'state' , 'number' , 'title' ] ;
18+ export class TableHttpExample implements OnInit {
19+ displayedColumns = [ 'created_at ' , 'state' , 'number' , 'title' ] ;
2220 exampleDatabase : ExampleHttpDao | null ;
2321 dataSource : ExampleDataSource | null ;
2422
2523 @ViewChild ( MdPaginator ) paginator : MdPaginator ;
2624 @ViewChild ( MdSort ) sort : MdSort ;
2725
28- constructor ( http : Http ) {
29- this . exampleDatabase = new ExampleHttpDao ( http ) ;
30- }
26+ constructor ( private http : Http ) { }
3127
3228 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 ) ;
3532 }
3633}
3734
35+ export interface GithubApi {
36+ items : GithubIssue [ ] ;
37+ total_count : number ;
38+ }
39+
3840export interface GithubIssue {
41+ created_at : string ;
3942 number : string ;
4043 state : string ;
4144 title : string ;
42- created : Date ;
4345}
4446
4547/** An example database that the data source uses to retrieve data for the table. */
4648export class ExampleHttpDao {
4749 constructor ( private http : Http ) { }
4850
49- getRepoIssues ( sort : string , order : string , page : number ) : Observable < Response > {
51+ getRepoIssues ( sort : string , order : string , page : number ) : Observable < GithubApi > {
5052 const href = 'https://api.github.com/search/issues' ;
5153 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 ) ;
5458 }
5559}
5660
@@ -63,67 +67,48 @@ export class ExampleHttpDao {
6367 */
6468export class ExampleDataSource extends DataSource < GithubIssue > {
6569 // The number of issues returned by github matching the query.
66- resultsLength : number = 0 ;
70+ resultsLength = 0 ;
6771 isLoadingResults : boolean ;
6872 isRateLimitReached : boolean ;
6973
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 ) {
7377 super ( ) ;
7478 }
7579
7680 /** Connect function called by the table to retrieve one stream containing the data to render. */
7781 connect ( ) : Observable < GithubIssue [ ] > {
7882 const displayDataChanges = [
79- this . _sort . mdSortChange ,
80- this . _paginator . page ,
83+ this . sort . mdSortChange ,
84+ this . paginator . page
8185 ] ;
8286
8387 // 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 ) ;
8789
8890 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+ } ) ;
115111 }
116112
117113 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- }
129114}
0 commit comments