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

Commit 88c52d1

Browse files
committed
No more new resources when new data is received, just same resource updated.
1 parent 72399d7 commit 88c52d1

5 files changed

Lines changed: 93 additions & 19 deletions

File tree

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

src/library/resource.ts

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ export class Resource implements IResource {
163163
return this.__exec(null, params, fc_success, fc_error, 'all');
164164
}
165165

166+
// just for debuggin purposes
167+
public getCachedResources() {
168+
return this.getService().memorycache.resources;
169+
}
170+
166171
public save<T extends IResource>(params?: Object | Function, fc_success?: Function, fc_error?: Function): Array<T> {
167172
return this.__exec(null, params, fc_success, fc_error, 'save');
168173
}
@@ -406,10 +411,32 @@ export class Resource implements IResource {
406411
this.getService().memorycache.clearAllCollections();
407412
}
408413

409-
this.id = success.data.data.id;
410-
411-
Converter.build(success.data, this, this.schema);
412-
this.getService().memorycache.setResource(this);
414+
// is a resource?
415+
if ('id' in success.data.data) {
416+
this.id = success.data.data.id;
417+
Converter.build(success.data, this, this.schema);
418+
/*
419+
Si lo guardo en la caché, luego no queda bindeado con la vista
420+
Usar {{ $ctrl.service.getCachedResources() | json }}, agregar uno nuevo, editar
421+
*/
422+
// this.getService().memorycache.setResource(this);
423+
} else if (angular.isArray(success.data.data)) {
424+
console.warn('Server return a collection when we save()', success.data.data);
425+
426+
/*
427+
we request the service again, because server maybe are giving
428+
us another type of resource (getService(resource.type))
429+
*/
430+
let tempororay_collection = this.getService().memorycache.getCollection('justAnUpdate');
431+
Converter.build(success.data, tempororay_collection, this.schema);
432+
angular.forEach(tempororay_collection, (resource_value: IResource, key: string) => {
433+
let res = Converter.getService(resource_value.type).memorycache.resources[resource_value.id];
434+
Converter.getService(resource_value.type).memorycache.setResource(resource_value);
435+
res.id = res.id + 'x';
436+
});
437+
438+
console.warn('Temporal collection for a resource_value update', tempororay_collection);
439+
}
413440

414441
this.runFc(fc_success, success);
415442
},
@@ -434,18 +461,14 @@ export class Resource implements IResource {
434461
this.relationships[type_alias] = { data: { } };
435462
}
436463

437-
if (this.schema.relationships[type_alias].hasMany) {
464+
if (type_alias in this.schema.relationships && this.schema.relationships[type_alias].hasMany) {
438465
this.relationships[type_alias]['data'][object_key] = resource;
439466
} else {
440467
this.relationships[type_alias]['data'] = resource;
441468
}
442469
}
443470

444471
public addRelationships(resources: ICollection, type_alias: string) {
445-
if (!this.schema.relationships[type_alias].hasMany) {
446-
console.warn('addRelationships not supported on ' + this.type + ' schema.');
447-
}
448-
449472
if (!(type_alias in this.relationships)) {
450473
this.relationships[type_alias] = { data: { } };
451474
} else {

src/library/services/memorycache.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ICollection, IResource } from '../interfaces';
22
import { ICache } from '../interfaces/cache.d';
33
import { Base } from './base';
4+
import { ResourceFunctions } from './resource-functions';
45

56
export class MemoryCache implements ICache {
67
private collections = {};
@@ -39,13 +40,19 @@ export class MemoryCache implements ICache {
3940
}
4041

4142
public setResource(resource: IResource): void {
42-
this.resources[resource.id] = resource;
43+
/*
44+
we cannot redefine object, because view don't update.
45+
*/
46+
if (resource.id in this.resources) {
47+
ResourceFunctions.resourceToResource(resource, this.resources[resource.id]);
48+
} else {
49+
this.resources[resource.id] = resource;
50+
}
4351
this.resources[resource.id].lastupdate = Date.now();
4452
}
4553

4654
public clearAllCollections(): boolean {
4755
this.collections = {};
48-
this.resources = {};
4956
this.collections_lastupdate = {};
5057
return true;
5158
}

src/library/services/resource-converter.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,27 @@ export class Converter {
118118
resource_dest.page.total_resources = document_from['meta']['total_resources'] ? document_from['meta']['total_resources'] : null;
119119
}
120120

121-
let resource: IDataResource;
122-
for (resource of document_from.data) {
123-
let service = Converter.getService(resource.type);
124-
if (!(resource.id in resource_dest)) {
125-
resource_dest[resource.id] = new (<any>service.constructor)();
126-
resource_dest[resource.id].reset();
121+
// convert and add new dataresoures to final collection
122+
let dataresource: IDataResource;
123+
let new_ids = {};
124+
for (dataresource of document_from.data) {
125+
let service = Converter.getService(dataresource.type);
126+
if (!(dataresource.id in resource_dest)) {
127+
resource_dest[dataresource.id] = new (<any>service.constructor)();
128+
resource_dest[dataresource.id].reset();
127129
}
128-
Converter._buildResource(resource, resource_dest[resource.id], schema, included_resources);
130+
Converter._buildResource(dataresource, resource_dest[dataresource.id], schema, included_resources);
131+
new_ids[dataresource.id] = dataresource.id;
129132
}
133+
134+
/*
135+
remove old members of collection (bug, for example, when request something like orders/10/details and has new ids)
136+
*/
137+
angular.forEach(resource_dest, resource => {
138+
if (!(resource.id in new_ids)) {
139+
delete resource_dest[resource.id];
140+
}
141+
});
130142
}
131143

132144
static _buildResource(
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { IResource } from '../interfaces';
2+
3+
export class ResourceFunctions {
4+
static resourceToResource(source: IResource, destination: IResource): void {
5+
destination.attributes = source.attributes;
6+
7+
// remove relationships on destination resource
8+
for (let type_alias in destination.relationships) {
9+
if (!(type_alias in source.relationships)) {
10+
delete destination.relationships[type_alias];
11+
} else {
12+
// this resource is a collection?
13+
if (!('id' in destination.relationships[type_alias].data)) {
14+
for (let id in destination.relationships[type_alias].data) {
15+
if (!(id in source.relationships[type_alias].data)) {
16+
delete destination.relationships[type_alias];
17+
}
18+
}
19+
}
20+
}
21+
}
22+
23+
// add source relationships to destination
24+
for (let type_alias in source.relationships) {
25+
if ('id' in source.relationships[type_alias].data) {
26+
destination.addRelationship(source.relationships[type_alias].data, source.relationships[type_alias].data.type);
27+
} else {
28+
destination.addRelationships(source.relationships[type_alias].data, type_alias);
29+
}
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)