2017-02-22 13:13:21 -05:00
|
|
|
// #docregion
|
2018-02-27 17:06:06 -05:00
|
|
|
import { HttpClient } from '@angular/common/http';
|
2018-07-25 06:12:40 -04:00
|
|
|
import { Pipe, PipeTransform } from '@angular/core';
|
|
|
|
|
2017-02-22 13:13:21 -05:00
|
|
|
// #docregion pipe-metadata
|
|
|
|
@Pipe({
|
|
|
|
name: 'fetch',
|
|
|
|
pure: false
|
|
|
|
})
|
|
|
|
// #enddocregion pipe-metadata
|
2018-07-25 06:12:40 -04:00
|
|
|
export class FetchJsonPipe implements PipeTransform {
|
2017-02-22 13:13:21 -05:00
|
|
|
private cachedData: any = null;
|
|
|
|
private cachedUrl = '';
|
|
|
|
|
2018-02-27 17:06:06 -05:00
|
|
|
constructor(private http: HttpClient) { }
|
2017-02-22 13:13:21 -05:00
|
|
|
|
|
|
|
transform(url: string): any {
|
|
|
|
if (url !== this.cachedUrl) {
|
|
|
|
this.cachedData = null;
|
|
|
|
this.cachedUrl = url;
|
2018-07-25 06:12:40 -04:00
|
|
|
this.http.get(url).subscribe(result => this.cachedData = result);
|
2017-02-22 13:13:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.cachedData;
|
|
|
|
}
|
|
|
|
}
|