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

Commit 5a0935e

Browse files
committed
better types on functions
1 parent 8857657 commit 5a0935e

5 files changed

Lines changed: 87 additions & 48 deletions

File tree

src/demo/books/books.component.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,29 @@ class BooksController {
3232
},
3333
success => {
3434
console.log('success books controller', success, this.books);
35+
36+
// TEST 1
37+
// this test merge data with cache (this not include author or photos)
38+
console.log('book#1', (<IDataResource>this.books[1].relationships.author.data).attributes);
39+
// this.books = this.BooksService.all();
40+
41+
// TEST 2
42+
// let book1 = this.BooksService.get(1,
43+
// success => {
44+
// book1.attributes.title += ' :)'; // update view
45+
// console.log('book1', (<IDataResource>book1.relationships.author.data).attributes);
46+
// });
3547
},
3648
error => {
3749
console.log('error books controller', error);
3850
}
3951
);
4052
}
4153

54+
public do() {
55+
this.BooksService.all();
56+
}
57+
4258
public delete(book: Jsonapi.IResource) {
4359
this.BooksService.delete(book.id);
4460
}

src/demo/books/books.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div>
22
<filter-textbox filter="$ctrl.filter"></filter-textbox>
33
<br/>
4-
<h3>Books</h3>
4+
<h3 ng-click="$ctrl.do()">Books</h3>
55
<table class="table table-striped">
66
<thead>
77
<tr>

src/library/resource.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ export class Resource implements IResource, IService {
215215
params.include ? path.setInclude(params.include) : null;
216216

217217
// cache
218-
let resource = (id in this.getService().memorycache.resources ? this.getService().memorycache.resources[id] : this.new());
218+
let resource = Converter.newResource(this.type, id);
219219
resource.is_loading = true;
220220
// exit if ttl is not expired
221221
let temporal_ttl = params.ttl ? params.ttl : 0;

src/library/services/resource-converter.ts

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ export class Converter {
3838
static json_array2resources_array_by_type (
3939
json_array: Array<IDataResource>,
4040
instance_relationships: boolean
41-
): Object { // Array<Jsonapi.IResource> {
41+
): Array<Jsonapi.IResource> {
4242
let all_resources:any = { } ;
4343
Converter.json_array2resources_array(json_array, all_resources, false);
44-
let resources = { };
44+
let resources = [];
4545
angular.forEach(all_resources, (resource) => {
4646
if (!(resource.type in resources)) {
4747
resources[resource.type] = { };
@@ -69,17 +69,37 @@ export class Converter {
6969
let resource_service = Core.Me.getResource(type);
7070
if (angular.isUndefined(resource_service)) {
7171
console.warn('`' + type + '`', 'service not found on getService()');
72+
// resource_service = new Resource();
73+
// resource_service.memorycache = new MemoryCache();
74+
// resource_service.reset();
7275
}
7376
return resource_service;
7477
}
7578

79+
static newResource(type: string, id: string): Jsonapi.IResource {
80+
if (Converter.getService(type).memorycache && id in Converter.getService(type).memorycache.resources) {
81+
return Converter.getService(type).memorycache.resources[id];
82+
} else {
83+
return Converter.getService(type).new();
84+
}
85+
}
86+
7687
/* return a resource type(resoruce_service) with data(data) */
7788
static procreate(resource_service: Jsonapi.IResource, data: IDataResource): Jsonapi.IResource {
7889
if (!('type' in data && 'id' in data)) {
7990
console.error('Jsonapi Resource is not correct', data);
8091
}
81-
let resource = new (<any>resource_service.constructor)();
82-
resource.new();
92+
93+
let resource: Jsonapi.IResource;
94+
if (data.id in Converter.getService(data.type).memorycache.resources) {
95+
console.log('HAY CACHE!', data.type);
96+
resource = Converter.getService(data.type).memorycache.resources[data.id];
97+
} else {
98+
// resource = new (<any>resource_service.constructor)();
99+
// resource.new();
100+
resource = Converter.getService(data.type).clone();
101+
}
102+
83103
resource.id = data.id;
84104
resource.attributes = data.attributes ? data.attributes : {};
85105
resource.is_new = false;
@@ -92,68 +112,66 @@ export class Converter {
92112
schema: Jsonapi.ISchema
93113
) {
94114
// instancio los include y los guardo en included arrary
95-
let included_resources = {};
115+
let included_resources = [];
96116
if ('included' in document_from) {
97117
included_resources = Converter.json_array2resources_array_by_type(document_from.included, false);
98118
}
99119

100120
if (angular.isArray(document_from.data)) {
101-
Converter._buildResources(document_from, <Jsonapi.ICollection>resource_dest, schema, included_resources);
121+
Converter._buildCollection(document_from, <Jsonapi.ICollection>resource_dest, schema, included_resources);
102122
} else {
103123
Converter._buildResource(document_from.data, <Jsonapi.IResource>resource_dest, schema, included_resources);
104124
}
105125
}
106126

107-
static _buildResources(
108-
document_from: Jsonapi.ICollection,
109-
resource_dest: Jsonapi.ICollection,
127+
static _buildCollection(
128+
collection_data_from: IDataCollection,
129+
collection_dest: Jsonapi.ICollection,
110130
schema: Jsonapi.ISchema,
111-
included_resources: Object
131+
included_resources: Jsonapi.IResource[]
112132
) {
113133
// sometime get Cannot set property 'number' of undefined (page)
114-
if (resource_dest.page && document_from['meta']) {
115-
resource_dest.page.number = document_from['meta']['page'] ? document_from['meta']['page'] : 1;
116-
resource_dest.page.
117-
resources_per_page = document_from['meta']['resources_per_page'] ? document_from['meta']['resources_per_page'] : null;
118-
resource_dest.page.total_resources = document_from['meta']['total_resources'] ? document_from['meta']['total_resources'] : null;
134+
if (collection_dest.page && collection_data_from['meta']) {
135+
collection_dest.page.number = collection_data_from['meta']['page'] || 1;
136+
collection_dest.page.resources_per_page = collection_data_from['meta']['resources_per_page'] || null;
137+
collection_dest.page.total_resources = collection_data_from['meta']['total_resources'] || null;
119138
}
120139

121140
// convert and add new dataresoures to final collection
122-
let dataresource: IDataResource;
123141
let new_ids = {};
124-
for (dataresource of document_from.data) {
142+
for (let dataresource of collection_data_from.data) {
125143
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();
144+
if (!(dataresource.id in collection_dest)) {
145+
collection_dest[dataresource.id] = new (<any>service.constructor)();
146+
collection_dest[dataresource.id].reset();
129147
}
130-
Converter._buildResource(dataresource, resource_dest[dataresource.id], schema, included_resources);
148+
Converter._buildResource(dataresource, collection_dest[dataresource.id], schema, included_resources);
131149
new_ids[dataresource.id] = dataresource.id;
132150
}
133151

134152
/*
135153
remove old members of collection (bug, for example, when request something like orders/10/details and has new ids)
136154
*/
137-
angular.forEach(resource_dest, resource => {
155+
angular.forEach(collection_dest, resource => {
138156
if (!(resource.id in new_ids)) {
139-
delete resource_dest[resource.id];
157+
delete collection_dest[resource.id];
140158
}
141159
});
142160
}
143161

144162
static _buildResource(
145-
document_from: IDataResource,
163+
resource_data_from: IDataResource,
146164
resource_dest: Jsonapi.IResource,
147165
schema: Jsonapi.ISchema,
148-
included_resources: Object
166+
included_resources: Jsonapi.IResource[]
149167
) {
150-
resource_dest.attributes = document_from.attributes;
151-
resource_dest.id = document_from.id;
168+
resource_dest.attributes = resource_data_from.attributes;
169+
resource_dest.id = resource_data_from.id;
152170
resource_dest.is_new = false;
153171

154172
let relationships_converter = new ResourceRelationshipsConverter(
155173
Converter.getService,
156-
document_from.relationships,
174+
resource_data_from.relationships,
157175
resource_dest.relationships,
158176
included_resources,
159177
schema

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

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ export class ResourceRelationshipsConverter {
66
private getService: Function;
77
private relationships_from: Array<any>;
88
private relationships_dest: IRelationships;
9-
private included_resources: Object;
9+
private included_resources: IResource[];
1010
private schema: ISchema;
1111

1212
/** @ngInject */
1313
public constructor(
1414
getService: Function,
1515
relationships_from: Array<any>,
1616
relationships_dest: IRelationships,
17-
included_resources: Object,
17+
included_resources: IResource[],
1818
schema: ISchema
1919
) {
2020
this.getService = getService;
@@ -96,36 +96,41 @@ export class ResourceRelationshipsConverter {
9696
}
9797

9898
private __buildRelationshipHasOne(
99-
relation_from_value: IDataObject,
100-
relation_key: number
101-
) {
99+
relation_data_from: IDataObject,
100+
relation_data_key: number
101+
): void {
102102
// new related resource <> cached related resource <> ? delete!
103103
if (
104-
this.relationships_dest[relation_key].data == null ||
105-
relation_from_value.data.id !== (<IResource>this.relationships_dest[relation_key].data).id
104+
this.relationships_dest[relation_data_key].data == null ||
105+
relation_data_from.data.id !== (<IResource>this.relationships_dest[relation_data_key].data).id
106106
) {
107-
this.relationships_dest[relation_key].data = {};
107+
this.relationships_dest[relation_data_key].data = {};
108108
}
109109

110110
// trae datos o cambió resource? actualizamos!
111111
if (
112-
'attributes' in relation_from_value.data ||
113-
(<IResource>this.relationships_dest[relation_key].data).id !== relation_from_value.data.id
112+
'attributes' in relation_data_from.data ||
113+
(<IResource>this.relationships_dest[relation_data_key].data).id !== relation_data_from.data.id
114114
) {
115-
let tmp = this.__buildRelationship(relation_from_value.data, this.included_resources);
116-
this.relationships_dest[relation_key].data = tmp;
115+
let resource_data = this.__buildRelationship(relation_data_from.data, this.included_resources);
116+
this.relationships_dest[relation_data_key].data = resource_data;
117117
}
118118
}
119119

120-
private __buildRelationship(relation: IDataResource, included_array): IResource | IDataResource {
121-
if (relation.type in included_array &&
122-
relation.id in included_array[relation.type]
120+
private __buildRelationship(resource_data_from: IDataResource, included_array: IResource[]): IResource | IDataResource {
121+
if (resource_data_from.type in included_array &&
122+
resource_data_from.id in included_array[resource_data_from.type]
123123
) {
124124
// it's in included
125-
return included_array[relation.type][relation.id];
125+
return included_array[resource_data_from.type][resource_data_from.id];
126126
} else {
127-
// resource not included, return directly the object
128-
return relation;
127+
// OPTIONAL: return cached IResource
128+
let service = this.getService(resource_data_from.type);
129+
if (service && resource_data_from.id in service.memorycache.resources) {
130+
return service.memorycache.resources[resource_data_from.id];
131+
} else {
132+
return resource_data_from;
133+
}
129134
}
130135
}
131136
}

0 commit comments

Comments
 (0)