Skip to content

Instantly share code, notes, and snippets.

@BenevidesLecontes
Created June 27, 2016 14:04
Show Gist options
  • Save BenevidesLecontes/21f18452e7658f3574d6d35d5352248b to your computer and use it in GitHub Desktop.
Save BenevidesLecontes/21f18452e7658f3574d6d35d5352248b to your computer and use it in GitHub Desktop.
/**
* Created by benevideschissanga on 5/9/16.
*/
import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {Observable} from 'rxjs/Observable';
@Injectable()
export class MyService {
private _videoUrl = 'api/videos.json';
private _feedProductsUrl = 'api/images.json';
private videoObservable = null;
private productsObservable = null;
constructor(private _http:Http) {
}
getFiles():any {
//noinspection TypeScriptUnresolvedFunction
if (this.productsObservable === null) {
this.productsObservable = this._http.get(this._feedProductsUrl)
.cache()
//.delay(2000)
.map(res => res.json())
.catch(this.handleError);
}
return this.productsObservable;
}
getVideo() {
// If we have account cached, use it instead
if (this.videoObservable === null) {
this.videoObservable = this._http.get(this._videoUrl)
.share()
.map(res => res.json())
.catch(this.handleError);
}
return this.videoObservable;
}
private handleError = (error:Response)=> {
console.error(error);
return Observable.throw(error.json().error || 'Server error');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment