import { Injectable } from '@angular/core'; import { Http } from '@angular/http'; import 'rxjs/add/operator/map'; /* Generated class for StoreDetailsProvider. See https://angular.io/docs/ts/latest/guide/dependency-injection.html for more info on providers and Angular 2 DI. */ @Injectable() export class StoreDetailsProvider { public store: any; constructor(public http: Http) {} load(currentProvider: string, storeId: number) { if (this.store) { // already loaded data return Promise.resolve(this.store); } // don't have the data yet return new Promise(resolve => { // We're using Angular Http provider to request the data, // then on the response it'll map the JSON data to a parsed JS object. // Next we process the data and resolve the promise with the new data. this.http.get(currentProvider + 'stores/' + storeId) .map(res => res.json()) .subscribe(data => { // we've got back the raw data, now generate the core schedule data // and save the data for later reference this.store = data; resolve(this.store); }, (err) => { console.error('Error fetching store details from API\n', err); }); }); } }