Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 1 | import {IXosAppConfig} from '../../../index'; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 2 | export interface IXosResourceService { |
| 3 | getResource(url: string): ng.resource.IResourceClass<any>; |
| 4 | } |
| 5 | |
| 6 | export class ModelRest implements IXosResourceService { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 7 | static $inject = ['$resource', 'AppConfig']; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 8 | |
| 9 | /** @ngInject */ |
| 10 | constructor( |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 11 | private $resource: ng.resource.IResourceService, |
| 12 | private AppConfig: IXosAppConfig |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 13 | ) { |
| 14 | |
| 15 | } |
| 16 | |
| 17 | public getResource(url: string): ng.resource.IResourceClass<ng.resource.IResource<any>> { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 18 | const resource: angular.resource.IResourceClass<any> = this.$resource(`${this.AppConfig.apiEndpoint}${url}/:id/`, {id: '@id'}, { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 19 | update: { method: 'PUT' }, |
| 20 | query: { |
| 21 | method: 'GET', |
| 22 | isArray: true, |
| 23 | transformResponse: (res) => { |
| 24 | // FIXME chameleon return everything inside "items" |
| 25 | return res.items ? res.items : res; |
| 26 | } |
| 27 | } |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 28 | }); |
| 29 | |
| 30 | resource.prototype.$save = function() { |
| 31 | if (this.id) { |
| 32 | return this.$update(); |
| 33 | } else { |
| 34 | return resource.save(this).$promise; |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | return resource; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 39 | } |
| 40 | } |