import { Injectable } from '@angular/core'; import { Http, Headers, Response, RequestOptions } from "@angular/http"; import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/catch'; import 'rxjs/add/operator/map'; {{#each collections}} {{#if methods}} //--------------------------------- // data class //--------------------------------- @Injectable() export class {{tsClassPropertyName this.name}} { {{#each model}} public {{@key}}: {{tsPropertyType this}}{{#if @last}}{{else}}; {{/if}} {{/each}} constructor(private http: Http) {} private serverUrl: string = "{{path}}"; public read(id: any): any { {{#if methods.get}} let options = this.createRequestOptions(); return this.http.get(this.serverUrl + "/" + id, options) .map( this.extractData(res) ); {{else}} return false; {{/if}} } public save(data: any): any { {{#if methods.post}} let options = this.createRequestOptions(); return this.http.post(this.serverUrl + "/" + id, data, options) .map( this.extractData(data) ); {{else}} return false; {{/if}} } public update(id: any, data: any): any { {{#if method.put}} this.save(id, data); {{else}} return false; {{/if}} } public remove(id: any): any { {{#if methods.delete}} let options = this.createRequestOptions(); return this.http.delete(this.serverUrl + "/" + id, {}, options) .map(res => res.json().data || { } ); {{else}} return false; {{/if}} } private extractData(res: Response) { let body = res.json(); return body.data || { }; } private createRequestOptions() { let headers = new Headers(); headers.append("AuthKey", "my-key"); headers.append("AuthToken", "my-token"); headers.append("Content-Type", "application/json"); let options = new RequestOptions({ headers: headers }); return options; } } {{/if}} {{/each}}