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

Commit 61a5aef

Browse files
committed
problems solved with memorycache. parseFromServer() parseToServer() added
1 parent 9589cad commit 61a5aef

File tree

10 files changed

+57
-27
lines changed

10 files changed

+57
-27
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 0.7.x
1+
# 0.7.x (planned)
22

33
- Interfaces are in `ng.jsonapi` now
44
- Resources can be extended for personalized classes
@@ -9,6 +9,7 @@
99

1010
- Save on localstore all data. When you request a resource or collection, first check memory. If its empty, read from store. If is empty, get the data from back-end.
1111
- HttpStorage deprecated: jsons were saved as sent by the server, now we save json with logic (saving ids and resources separately).
12+
- Service with `toServer()` and `fromServer()` functions. They execute before and after http request. Ideal for type conversions.
1213

1314
## No more declaration file .d.ts
1415

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.2",
3+
"version": "0.6.15",
44
"description": "JSONAPI library developed for AngularJS in Typescript",
55
"repository": {
66
"type": "git",

src/demo/authors/authors.service.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,14 @@ export class AuthorsService extends Jsonapi.Service {
2222
},
2323
ttl: 10
2424
};
25+
26+
// executed before get data from server
27+
public parseFromServer(attributes): void {
28+
attributes.name = attributes.name + ' ♥';
29+
}
30+
31+
// executed before send to server
32+
public parseToServer(attributes): void {
33+
attributes.name = attributes.name.replace('♥', '').trim();
34+
}
2535
}

src/library/interfaces/cachememory.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ import { ICache } from '../interfaces/cache.d';
44
export interface ICacheMemory extends ICache {
55
resources: { [id: string]: IResource };
66

7-
getResource(id: string): IResource;
87
getOrCreateCollection(url: string): ICollection;
98
isCollectionExist(url: string): boolean;
109
isCollectionLive(url: string, ttl: number): boolean;
1110
clearAllCollections(): boolean;
1211

1312
isResourceLive(id: string, ttl: number): boolean;
1413
getOrCreateResource(type: string, id: string): IResource;
15-
getResource(id: string): IResource;
1614

1715
removeResource(id: string): void;
1816
}

src/library/interfaces/service.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ISchema, IResource, ICollection, ICacheMemory, ICacheStore, IParamsCollection, IParamsResource } from './index';
1+
import { ISchema, IResource, ICollection, ICacheMemory, IAttributes, ICacheStore, IParamsCollection, IParamsResource } from './index';
22

33
export interface IService {
44
type: string;
@@ -14,4 +14,6 @@ export interface IService {
1414
new?<T extends IResource>(): T;
1515
cachememory: ICacheMemory;
1616
cachestore: ICacheStore;
17+
parseFromServer(attributes: IAttributes): void;
18+
parseToServer(attributes: IAttributes): void;
1719
}

src/library/resource.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,14 @@ export class Resource extends ParentResourceService implements IResource {
8484
}
8585
});
8686

87+
let attributes = angular.copy(this.attributes);
88+
this.getService().parseToServer(attributes);
89+
8790
let ret: IDataObject = {
8891
data: {
8992
type: this.type,
9093
id: this.id,
91-
attributes: this.attributes,
94+
attributes: attributes,
9295
relationships: relationships
9396
}
9497
};

src/library/service.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { CacheMemory } from './services/cachememory';
1111
import { CacheStore } from './services/cachestore';
1212

1313
import { IService, ISchema, IResource, ICollection, IExecParams, ICacheStore, ICacheMemory,
14-
IParamsCollection, IParamsResource } from './interfaces';
14+
IParamsCollection, IParamsResource, IAttributes } from './interfaces';
1515

1616
export class Service extends ParentResourceService implements IService {
1717
public schema: ISchema;
@@ -37,8 +37,13 @@ export class Service extends ParentResourceService implements IService {
3737
return Core.me._register(this);
3838
}
3939

40-
public new<T extends IResource>(): T {
40+
public newResource(): IResource {
4141
let resource: IResource = new Resource();
42+
return resource;
43+
}
44+
45+
public new<T extends IResource>(): T {
46+
let resource = this.newResource();
4247
resource.type = this.type;
4348
resource.reset();
4449
return <T>resource;
@@ -145,7 +150,10 @@ export class Service extends ParentResourceService implements IService {
145150
let path = new PathBuilder();
146151
let paramsurl = new UrlParamsBuilder();
147152
path.applyParams(this, params);
148-
params.remotefilter ? path.addParam(paramsurl.toparams( { filter: params.remotefilter } )) : null;
153+
if (params.remotefilter && Object.keys(params.remotefilter).length > 0) {
154+
this.getService().parseToServer(params.remotefilter);
155+
path.addParam(paramsurl.toparams( { filter: params.remotefilter } ));
156+
}
149157
if (params.page) {
150158
params.page.number > 1 ? path.addParam(
151159
Core.injectedServices.rsJsonapiConfig.parameters.page.number + '=' + params.page.number) : null;
@@ -194,6 +202,7 @@ export class Service extends ParentResourceService implements IService {
194202
}
195203
} else {
196204
// STORE
205+
tempororay_collection.$is_loading = true;
197206
this.getService().cachestore.getCollectionFromStorePromise(path.getForCache(), tempororay_collection)
198207
.then(
199208
success => {
@@ -220,6 +229,7 @@ export class Service extends ParentResourceService implements IService {
220229

221230
private getAllFromServer(path, params, fc_success, fc_error, tempororay_collection: ICollection, cached_collection: ICollection) {
222231
// SERVER REQUEST
232+
tempororay_collection.$is_loading = true;
223233
Core.injectedServices.JsonapiHttp
224234
.get(path.get())
225235
.then(
@@ -286,4 +296,12 @@ export class Service extends ParentResourceService implements IService {
286296
public clearCacheMemory(): boolean {
287297
return this.getService().cachememory.clearAllCollections();
288298
}
299+
300+
public parseFromServer(attributes: IAttributes): void {
301+
302+
}
303+
304+
public parseToServer(attributes: IAttributes): void {
305+
306+
}
289307
}

src/library/services/cachememory.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,24 @@ export class CacheMemory implements ICacheMemory {
4343

4444
public getOrCreateResource(type: string, id: string): IResource {
4545
if (Converter.getService(type).cachememory && id in Converter.getService(type).cachememory.resources) {
46-
return Converter.getService(type).cachememory.getResource(id);
46+
return Converter.getService(type).cachememory.resources[id];
4747
} else {
4848
let resource = Converter.getService(type).new();
4949
resource.id = id;
50+
// needed for a lot of request (all and get, tested on multinexo.com)
51+
this.setResource(resource, false);
5052
return resource;
5153
}
5254
}
5355

54-
/* @deprecated */
55-
public getResource(id: string): IResource {
56-
return this.resources[id];
57-
}
58-
59-
public setResource(resource: IResource): void {
56+
public setResource(resource: IResource, update_lastupdate = false): void {
6057
// we cannot redefine object, because view don't update.
6158
if (resource.id in this.resources) {
6259
ResourceFunctions.resourceToResource(resource, this.resources[resource.id]);
6360
} else {
6461
this.resources[resource.id] = resource;
6562
}
66-
this.resources[resource.id].lastupdate = Date.now();
63+
this.resources[resource.id].lastupdate = (update_lastupdate ? Date.now() : 0);
6764
}
6865

6966
public clearAllCollections(): boolean {

src/library/services/converter.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,14 @@ export class Converter {
7676
resource = Converter.getService(data.type).cachememory.getOrCreateResource(data.type, data.id);
7777
}
7878

79-
resource.attributes = data.attributes ? data.attributes : {};
79+
resource.attributes = data.attributes || {};
8080
resource.is_new = false;
8181
return resource;
8282
}
8383

8484
public static build(
8585
document_from: IDataCollection & IDataObject,
86-
resource_dest: IResource | ICollection,
87-
build_relationships = true
86+
resource_dest: IResource | ICollection
8887
) {
8988
// instancio los include y los guardo en included arrary
9089
let included_resources: IResourcesByType = {};
@@ -95,15 +94,14 @@ export class Converter {
9594
if (angular.isArray(document_from.data)) {
9695
Converter._buildCollection(document_from, <ICollection>resource_dest, included_resources);
9796
} else {
98-
build_relationships ? Converter._buildResource(document_from.data, <IResource>resource_dest, included_resources) : null;
97+
Converter._buildResource(document_from.data, <IResource>resource_dest, included_resources);
9998
}
10099
}
101100

102101
private static _buildCollection(
103102
collection_data_from: IDataCollection,
104103
collection_dest: ICollection,
105-
included_resources: IResourcesByType,
106-
build_relationships = true
104+
included_resources: IResourcesByType
107105
) {
108106
// sometime get Cannot set property 'number' of undefined (page)
109107
if (collection_dest.page && collection_data_from['meta']) {
@@ -119,7 +117,7 @@ export class Converter {
119117
collection_dest[dataresource.id] =
120118
Converter.getService(dataresource.type).cachememory.getOrCreateResource(dataresource.type, dataresource.id);
121119
}
122-
build_relationships ? Converter._buildResource(dataresource, collection_dest[dataresource.id], included_resources) : null;
120+
Converter._buildResource(dataresource, collection_dest[dataresource.id], included_resources);
123121
new_ids[dataresource.id] = dataresource.id;
124122
}
125123

@@ -137,6 +135,9 @@ export class Converter {
137135
included_resources: IResourcesByType
138136
) {
139137
resource_dest.attributes = resource_data_from.attributes;
138+
139+
Converter.getService(resource_data_from.type).parseFromServer(resource_dest.attributes);
140+
140141
resource_dest.id = resource_data_from.id;
141142
resource_dest.is_new = false;
142143
let service = Converter.getService(resource_data_from.type);

src/library/sources/http.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ export class Http {
1616

1717
}
1818

19-
public delete(path: string) {
19+
public delete(path: string): ng.IPromise<IDataObject> {
2020
return this.exec(path, 'DELETE');
2121
}
2222

23-
public get(path: string) {
23+
public get(path: string): ng.IPromise<IDataObject> {
2424
return this.exec(path, 'get');
2525
}
2626

27-
protected exec(path: string, method: string, data?: IDataObject, call_loadings_error:boolean = true) {
27+
protected exec(path: string, method: string, data?: IDataObject, call_loadings_error:boolean = true): ng.IPromise<IDataObject> {
2828

2929
// http request (if we don't have any GET request yet)
3030
if (method !== 'get' || !this.noDuplicatedHttpCallsService.hasPromises(path)) {

0 commit comments

Comments
 (0)