Skip to content
This repository was archived by the owner on Aug 18, 2018. It is now read-only.

Commit c4e0ee5

Browse files
committed
resources return a promise now.
1 parent 871abda commit c4e0ee5

File tree

5 files changed

+13
-11
lines changed

5 files changed

+13
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- HttpStorage deprecated: jsons were saved as sent by the server, now we save json with logic (saving ids and resources separately).
1212
- Service with `toServer()` and `fromServer()` functions. They execute before and after http request. Ideal for type conversions.
1313
- `JsonapiCore.duplicateResource(resouce, ...relationtypes)` return a duplication of resource. You can duplicate resources and, optionally, their relationships. (v0.6.16)
14+
- resource save() method return a promise.
1415

1516
## No more declaration file .d.ts
1617

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ts-angular-jsonapi",
3-
"version": "0.6.26",
3+
"version": "0.6.28",
44
"description": "JSONAPI library developed for AngularJS in Typescript",
55
"repository": {
66
"type": "git",

src/library/interfaces/resource.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export interface IResource {
1818
addRelationships? (resources: ICollection, type_alias: string): void;
1919
removeRelationship? (type_alias: string, id: string): boolean;
2020
addRelationshipsArray <T extends IResource>(resources: Array<T>, type_alias?: string): void;
21-
save<T extends IResource>(params?: IParamsResource, fc_success?: Function, fc_error?: Function): T;
21+
save<T extends IResource>(params?: IParamsResource, fc_success?: Function, fc_error?: Function): ng.IPromise<object>;
2222
toObject? (params?: IParamsResource): IDataObject;
2323
getService(): IService;
2424
}

src/library/parent-resource-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export class ParentResourceService {
66
/**
77
This method sort params for all(), get(), delete() and save()
88
*/
9-
protected __exec(exec_params: IExecParams): IResource | ICollection | void {
9+
protected __exec(exec_params: IExecParams): IResource | ICollection | void | ng.IPromise<object> {
1010
// makes `params` optional
1111
if (angular.isFunction(exec_params.params)) {
1212
exec_params.fc_error = exec_params.fc_success;

src/library/resource.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,11 @@ export class Resource extends ParentResourceService implements IResource {
109109
return ret;
110110
}
111111

112-
public save<T extends IResource>(params?: Object | Function, fc_success?: Function, fc_error?: Function): T {
113-
return <T>this.__exec({ id: null, params: params, fc_success: fc_success, fc_error: fc_error, exec_type: 'save' });
112+
public save<T extends IResource>(params?: Object | Function, fc_success?: Function, fc_error?: Function): ng.IPromise<object> {
113+
return this.__exec({ id: null, params: params, fc_success: fc_success, fc_error: fc_error, exec_type: 'save' });
114114
}
115115

116-
protected __exec(exec_params: IExecParams): IResource {
116+
protected __exec<T extends IResource>(exec_params: IExecParams): ng.IPromise<object> {
117117
super.__exec(exec_params);
118118

119119
switch (exec_params.exec_type) {
@@ -122,7 +122,9 @@ export class Resource extends ParentResourceService implements IResource {
122122
}
123123
}
124124

125-
private _save(params: IParamsResource, fc_success: Function, fc_error: Function): IResource {
125+
private _save<T extends IResource>(params: IParamsResource, fc_success: Function, fc_error: Function): ng.IPromise<object> {
126+
var deferred = Core.injectedServices.$q.defer();
127+
126128
if (this.is_saving || this.is_loading) {
127129
return ;
128130
}
@@ -135,8 +137,6 @@ export class Resource extends ParentResourceService implements IResource {
135137
path.applyParams(this.getService(), params);
136138
this.id && path.appendPath(this.id);
137139

138-
let resource = this.getService().cachememory.getOrCreateResource(this.type, this.id);
139-
140140
let promise = Core.injectedServices.JsonapiHttp.exec(
141141
path.get(), this.id ? 'PUT' : 'POST',
142142
object, !(angular.isFunction(fc_error))
@@ -181,15 +181,16 @@ export class Resource extends ParentResourceService implements IResource {
181181
}
182182

183183
this.runFc(fc_success, success);
184+
deferred.resolve(success);
184185
},
185186
error => {
186187
this.is_saving = false;
187-
188188
this.runFc(fc_error, 'data' in error ? error.data : error);
189+
deferred.reject('data' in error ? error.data : error);
189190
}
190191
);
191192

192-
return resource;
193+
return deferred.promise;
193194
}
194195

195196
public addRelationship<T extends IResource>(resource: T, type_alias?: string) {

0 commit comments

Comments
 (0)