2015-11-02 03:28:38 -05:00
|
|
|
// #docregion
|
2015-12-28 14:52:41 -05:00
|
|
|
import {Pipe, PipeTransform} from 'angular2/core';
|
2016-01-13 17:00:43 -05:00
|
|
|
import {Http} from 'angular2/http';
|
2015-11-02 03:28:38 -05:00
|
|
|
|
|
|
|
// #docregion pipe-metadata
|
|
|
|
@Pipe({
|
|
|
|
name: 'fetch',
|
|
|
|
pure: false
|
|
|
|
})
|
|
|
|
// #enddocregion pipe-metadata
|
2015-12-28 14:52:41 -05:00
|
|
|
export class FetchJsonPipe implements PipeTransform{
|
2016-01-13 17:00:43 -05:00
|
|
|
private fetched:any = null;
|
|
|
|
private prevUrl = '';
|
2015-12-13 01:01:46 -05:00
|
|
|
|
2016-01-13 17:00:43 -05:00
|
|
|
constructor(private _http: Http) { }
|
|
|
|
|
|
|
|
transform(url:string):any {
|
|
|
|
if (url !== this.prevUrl) {
|
|
|
|
this.prevUrl = url;
|
|
|
|
this.fetched = null;
|
|
|
|
this._http.get(url)
|
|
|
|
.map( result => result.json() )
|
|
|
|
.subscribe( result => this.fetched = result )
|
2015-11-02 03:28:38 -05:00
|
|
|
}
|
|
|
|
|
2016-01-13 17:00:43 -05:00
|
|
|
return this.fetched;
|
2015-11-02 03:28:38 -05:00
|
|
|
}
|
2016-01-13 17:00:43 -05:00
|
|
|
}
|