forked from ulfryk/angular-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathat-angular-resource.ts
67 lines (58 loc) · 2.83 KB
/
at-angular-resource.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* istanbul ignore next */
module at {
'use strict';
/* tslint:disable:no-any */
type ResourceClass = angular.resource.IResourceClass<any>;
type ResourceArray = angular.resource.IResourceArray<any>;
type ResourceService = angular.resource.IResourceService;
/* istanbul ignore next */
function combineResource(instance: any, model?: any): void {
angular.extend(instance, new instance.$_Resource(model));
}
/* istanbul ignore next */
export class Resource implements angular.resource.IResource<Resource> {
public static get: (params?: Object) => Resource;
public static query: (params?: Object) => ResourceArray;
public static remove: () => Resource;
public static save: () => Resource;
public static delete: () => Resource;
constructor(model?: any) { combineResource(this, model); }
public $get: (params?: Object) => angular.IPromise<this>;
public $query: (params?: Object) => angular.IPromise<angular.resource.IResourceArray<this>>;
public $remove: (params?: Object) => angular.IPromise<this>;
public $save: (params?: Object) => angular.IPromise<this>;
public $delete: (params?: Object) => angular.IPromise<this>;
public $promise: angular.IPromise<this>;
public $resolved: boolean;
public toJSON: () => {
[index: string]: any;
};
}
/* istanbul ignore next */
export class ResourceWithUpdate extends Resource {
constructor(model?: any) { super(model); }
public static update: () => ResourceWithUpdate;
public $update: () => angular.IPromise<this>;
public $promise : angular.IPromise<this>;
}
export interface IResourceAnnotation {
(moduleName: string, className: string): IClassAnnotationDecorator;
}
export function resource(moduleName: string, className: string): IClassAnnotationDecorator {
return (target: any): void => {
function resourceClassFactory($resource: ResourceService, ...args: any[]): any {
const newResource: ResourceClass = $resource(target.url, target.params, target.actions, target.options);
return attachInjects(angular.extend(newResource, angular.extend(target, newResource, {
prototype: angular.extend(newResource.prototype, angular.extend(target.prototype, {
/* tslint:disable:variable-name */
$_Resource: newResource
/* tslint:enable:variable-name */
}))
})), ...args);
}
resourceClassFactory.$inject = (['$resource']).concat(target.$inject /* istanbul ignore next */ || []);
angular.module(moduleName).factory(className, resourceClassFactory);
};
}
/* tslint:enable:no-any */
}