2015-12-13 01:01:46 -05:00
|
|
|
/// <reference path='./window.extension.d.ts'/>
|
2015-11-02 03:28:38 -05:00
|
|
|
// #docregion
|
2015-12-28 14:52:41 -05:00
|
|
|
import {Pipe, PipeTransform} from 'angular2/core';
|
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{
|
2015-11-02 03:28:38 -05:00
|
|
|
private fetchedValue:any;
|
|
|
|
private fetchPromise:Promise<any>;
|
2015-12-13 01:01:46 -05:00
|
|
|
|
2015-11-02 03:28:38 -05:00
|
|
|
transform(value:string, args:string[]):any {
|
|
|
|
if (!this.fetchPromise) {
|
|
|
|
this.fetchPromise = window.fetch(value)
|
2015-12-13 01:01:46 -05:00
|
|
|
.then((result:any) => result.json())
|
|
|
|
.then((json:any) => this.fetchedValue = json);
|
2015-11-02 03:28:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.fetchedValue;
|
|
|
|
}
|
|
|
|
}
|