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

Commit ee795ce

Browse files
committed
mode offline with relationships done!
1 parent 92e16a9 commit ee795ce

4 files changed

Lines changed: 23 additions & 19 deletions

File tree

src/library/resource.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,10 @@ export class Resource extends ParentResourceService implements IResource {
3838

3939
public toObject(params?: IParamsResource): IDataObject {
4040
params = angular.extend({}, Base.Params, params);
41-
// this.getService().schema = angular.extend({}, Base.Schema, this.getService().schema);
4241

43-
let relationships = { };
44-
let included = [ ];
45-
let included_ids = [ ]; // just for control don't repeat any resource
42+
let relationships = {};
43+
let included = [];
44+
let included_ids = []; // just for control don't repeat any resource
4645

4746
// REALTIONSHIPS
4847
angular.forEach(this.relationships, (relationship: IRelationship, relation_alias) => {
@@ -128,7 +127,6 @@ export class Resource extends ParentResourceService implements IResource {
128127
this.id && path.appendPath(this.id);
129128

130129
let resource = this.getService().cachememory.getOrCreateResource(this.type, this.id);
131-
// Converter.getOrCreateResource(this.type, this.id);
132130

133131
let promise = Core.injectedServices.JsonapiHttp.exec(
134132
path.get(), this.id ? 'PUT' : 'POST',

src/library/services/cachememory.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,15 @@ export class CacheMemory implements ICache {
9696
// -------- STORE ---------------------------------
9797

9898
public getResourceFromStore(resource: IResource): Promise<any> {
99-
let promise = this.fetchResourceFromStore(resource);
100-
promise.then (success => {
99+
let promise = Core.injectedServices.JsonapiCacheStore.getObjet(resource.type + '.' + resource.id);
100+
promise.then(success => {
101101
if (success) {
102102
Converter.build({ data: success }, resource);
103-
console.log('recibí resource del cachestore, actualizo', resource);
104103
}
105104
});
106105
return promise;
107106
}
108107

109-
private fetchResourceFromStore(resource: IResource): Promise<any> {
110-
return Core.injectedServices.JsonapiCacheStore.getObjet(resource.type + '.' + resource.id);
111-
}
112-
113108
private saveResourceStore(resource: IResource) {
114109
Core.injectedServices.JsonapiCacheStore.saveObject(
115110
resource.type + '.' + resource.id,
@@ -121,6 +116,7 @@ export class CacheMemory implements ICache {
121116
let promise = Core.injectedServices.JsonapiCacheStore.getObjet('collection.' + url);
122117
promise.then(success => {
123118
if (success) {
119+
// build collection from store and resources from memory
124120
let all_ok = true;
125121
for (let key in success.data) {
126122
let dataresource: IDataResource = success.data[key];
@@ -131,13 +127,12 @@ export class CacheMemory implements ICache {
131127
}
132128
collection[dataresource.id] = resource;
133129
}
134-
135-
// collection full with resources
136130
if (all_ok) {
137131
collection.$source = 'cachestore'; // collection from cachestore, resources from memory
138132
return;
139133
}
140134

135+
// request resources from store
141136
let temporalcollection = {};
142137
let promises = [];
143138
for (let key in success.data) {
@@ -148,7 +143,7 @@ export class CacheMemory implements ICache {
148143
);
149144
}
150145

151-
// we have all resources from store
146+
// build collection and resources from store
152147
Core.injectedServices.$q.all(promises).then(success => {
153148
// just for precaution, we not rewrite server data
154149
if (collection.$source !== 'new') {

src/library/services/converter.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ export class Converter {
8383

8484
public static build(
8585
document_from: IDataCollection & IDataObject,
86-
resource_dest: IResource | ICollection
86+
resource_dest: IResource | ICollection,
87+
build_relationships = true
8788
) {
8889
// instancio los include y los guardo en included arrary
8990
let included_resources: IResourcesByType = {};
@@ -94,14 +95,15 @@ export class Converter {
9495
if (angular.isArray(document_from.data)) {
9596
Converter._buildCollection(document_from, <ICollection>resource_dest, included_resources);
9697
} else {
97-
Converter._buildResource(document_from.data, <IResource>resource_dest, included_resources);
98+
build_relationships ? Converter._buildResource(document_from.data, <IResource>resource_dest, included_resources) : null;
9899
}
99100
}
100101

101102
private static _buildCollection(
102103
collection_data_from: IDataCollection,
103104
collection_dest: ICollection,
104-
included_resources: IResourcesByType
105+
included_resources: IResourcesByType,
106+
build_relationships = true
105107
) {
106108
// sometime get Cannot set property 'number' of undefined (page)
107109
if (collection_dest.page && collection_data_from['meta']) {
@@ -117,7 +119,7 @@ export class Converter {
117119
collection_dest[dataresource.id] =
118120
Converter.getService(dataresource.type).cachememory.getOrCreateResource(dataresource.type, dataresource.id);
119121
}
120-
Converter._buildResource(dataresource, collection_dest[dataresource.id], included_resources);
122+
build_relationships ? Converter._buildResource(dataresource, collection_dest[dataresource.id], included_resources) : null;
121123
new_ids[dataresource.id] = dataresource.id;
122124
}
123125

@@ -139,6 +141,11 @@ export class Converter {
139141
resource_dest.is_new = false;
140142
let schema = Converter.getService(resource_data_from.type).schema;
141143

144+
// esto previene la creación indefinida de resources
145+
if (!resource_dest.relationships) {
146+
return;
147+
}
148+
142149
let relationships_converter = new ResourceRelationshipsConverter(
143150
Converter.getService,
144151
resource_data_from.relationships,

src/library/services/resource-relationships-converter.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ export class ResourceRelationshipsConverter {
133133
if (service && resource_data_from.id in service.cachememory.resources) {
134134
return service.cachememory.resources[resource_data_from.id];
135135
} else {
136+
// we dont have information on included or memory. try pass to store
137+
if (service) {
138+
service.cachememory.getResourceFromStore(resource_data_from, false);
139+
}
136140
return resource_data_from;
137141
}
138142
}

0 commit comments

Comments
 (0)