import { Injectable, Inject } from "ng-forward"; @Injectable() @Inject("Restangular", "$q") export class EnvironmentService { private _currentEnvironmentPromise: ng.IDeferred; constructor(private restangular: restangular.IService, private $q: ng.IQService) { } getEnvironmentPeople(params: any): ng.IPromise { let p = this.restangular.one('people').get(params); let deferred = this.$q.defer(); p.then(this.getHandleSuccessFunctionKeyArray("people", deferred)); p.catch(this.getHandleErrorFunction(deferred)); return deferred.promise; } getByIdentifier(identifier: string): ng.IPromise { let p = this.restangular.one('environment').customGET(identifier); let deferred = this.$q.defer(); p.then(this.getHandleSuccessFunction(deferred)); p.catch(this.getHandleErrorFunction(deferred)); return deferred.promise; } getBoxes(id: number) { let p = this.restangular.one('environments', id).customGET("boxes"); let deferred = this.$q.defer(); p.then(this.getHandleSuccessFunctionKeyArray("boxes", deferred)); p.catch(this.getHandleErrorFunction(deferred)); return deferred.promise; } /** TODO - Please, use the base class RestangularService * (description) * * @template T * @param {ng.IDeferred} deferred (description) * @returns {(response: restangular.IResponse) => void} (description) */ getHandleErrorFunction(deferred: ng.IDeferred): (response: restangular.IResponse) => void { let self = this; /** * (description) * * @param {restangular.IResponse} response (description) */ let errorFunction = (response: restangular.IResponse): void => { deferred.reject(response); }; return errorFunction; } /** * TODO - use restangular service as base class, and this will not be necessary here anymore */ protected getHandleSuccessFunction(deferred: ng.IDeferred, responseKey?: string): (response: restangular.IResponse) => void { let self = this; /** * (description) * * @param {restangular.IResponse} response (description) */ let successFunction = (response: restangular.IResponse): void => { let data = this.restangular.stripRestangular(response.data); deferred.resolve(data); }; return successFunction; } /** * TODO - use restangular service as base class, and this will not be necessary here anymore */ protected getHandleSuccessFunctionKeyArray(key: string, deferred: ng.IDeferred, responseKey?: string): (response: restangular.IResponse) => void { let self = this; /** * (description) * * @param {restangular.IResponse} response (description) */ let successFunction = (response: restangular.IResponse): void => { let data = this.restangular.stripRestangular(response.data[key]); deferred.resolve(data); }; return successFunction; } }