| 
									
										
										
										
											2015-11-02 00:28:38 -08:00
										 |  |  | // #docregion
 | 
					
						
							| 
									
										
										
										
											2016-05-03 14:06:32 +02:00
										 |  |  | import { Pipe, PipeTransform } from '@angular/core'; | 
					
						
							|  |  |  | import { Http }                from '@angular/http'; | 
					
						
							| 
									
										
										
										
											2015-11-02 00:28:38 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // #docregion pipe-metadata
 | 
					
						
							|  |  |  | @Pipe({ | 
					
						
							|  |  |  |   name: 'fetch', | 
					
						
							|  |  |  |   pure: false | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | // #enddocregion pipe-metadata
 | 
					
						
							| 
									
										
										
										
											2016-06-08 01:06:25 +02:00
										 |  |  | export class FetchJsonPipe  implements PipeTransform { | 
					
						
							| 
									
										
										
										
											2016-05-13 13:44:14 -07:00
										 |  |  |   private fetchedJson: any = null; | 
					
						
							| 
									
										
										
										
											2016-01-13 15:00:43 -07:00
										 |  |  |   private prevUrl = ''; | 
					
						
							| 
									
										
										
										
											2015-12-12 22:01:46 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-13 15:00:43 -07:00
										 |  |  |   constructor(private _http: Http) { } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-21 01:12:21 +01:00
										 |  |  |   transform(url: string): any { | 
					
						
							| 
									
										
										
										
											2016-01-13 15:00:43 -07:00
										 |  |  |     if (url !== this.prevUrl) { | 
					
						
							|  |  |  |       this.prevUrl = url; | 
					
						
							| 
									
										
										
										
											2016-05-13 13:44:14 -07:00
										 |  |  |       this.fetchedJson = null; | 
					
						
							| 
									
										
										
										
											2016-01-13 15:00:43 -07:00
										 |  |  |       this._http.get(url) | 
					
						
							|  |  |  |         .map( result => result.json() ) | 
					
						
							| 
									
										
										
										
											2016-05-13 13:44:14 -07:00
										 |  |  |         .subscribe( result => this.fetchedJson = result ); | 
					
						
							| 
									
										
										
										
											2015-11-02 00:28:38 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-13 13:44:14 -07:00
										 |  |  |     return this.fetchedJson; | 
					
						
							| 
									
										
										
										
											2015-11-02 00:28:38 -08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-01-13 15:00:43 -07:00
										 |  |  | } |