Fix configuration loading
All checks were successful
FarmMaps.Develop/FarmMapsLib/develop This commit looks good

This commit is contained in:
Willem Dantuma 2019-12-19 17:03:10 +01:00
parent fd8025c674
commit 677608c14d

View File

@ -1,33 +1,35 @@
import {Inject, Injectable} from '@angular/core'; import {Inject, Injectable} from '@angular/core';
import { Location } from '@angular/common'; import { Location,PathLocationStrategy } from '@angular/common';
import {HttpClient, HttpXhrBackend} from '@angular/common/http'; import {HttpClient, HttpXhrBackend} from '@angular/common/http';
import {Observable} from 'rxjs'; import {Observable} from 'rxjs';
@Injectable() @Injectable()
export class AppConfig { export class AppConfig {
private config: Object = null; private config: Object = null;
private httpClient: HttpClient; private httpClient: HttpClient;
constructor(xhrBackend: HttpXhrBackend,private location:Location) { constructor(xhrBackend: HttpXhrBackend,private location:Location) {
this.httpClient = new HttpClient(xhrBackend); this.httpClient = new HttpClient(xhrBackend);
this.config = null; this.config = null;
} }
public getConfig(key: any) { public getConfig(key: any) {
if (!this.config.hasOwnProperty(key)) { if (!this.config.hasOwnProperty(key)) {
console.error(`Config key ${key} not set`); console.error(`Config key ${key} not set`);
} }
return this.config[key]; return this.config[key];
} }
public load(): Promise<any> { public load(): Promise<any> {
return this.httpClient.get(this.location.prepareExternalUrl('/configuration.json')) var url = this.location.prepareExternalUrl('/configuration.json');
.toPromise() if(url.startsWith("#")) url='/configuration.json';
.then(data => { return this.httpClient.get(url)
this.config = data; .toPromise()
//return data; .then(data => {
}) this.config = data;
.catch(error => this.config = null); //return data;
}; })
} .catch(error => this.config = null);
};
}