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

Commit 7f96146

Browse files
committed
_exec work with a object now (IExecParams). ResourceMerger fixed. Better variables names
1 parent 3dbc8a0 commit 7f96146

11 files changed

Lines changed: 65 additions & 51 deletions

File tree

src/demo/books/books.component.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,34 @@ class BooksController {
3535

3636
// TEST 1
3737
// this test merge data with cache (this not include author or photos)
38-
console.log('book#1', (<IDataResource>this.books[1].relationships.author.data));
39-
console.log('book#1', (<IDataResource>this.books[1].relationships.author.data).attributes);
40-
// this.books = this.BooksService.all();
38+
console.log('BooksRequest#1 received (author data from server)',
39+
(<IDataResource>this.books[1].relationships.author.data).attributes
40+
);
41+
42+
console.log('BooksRequest#2 requested');
43+
let books2 = this.BooksService.all(
44+
success => {
45+
console.log('BooksRequest#2 received (author data from cache)',
46+
(<IDataResource>books2[1].relationships.author.data)
47+
);
48+
}
49+
);
4150

4251
// TEST 2
43-
// let book1 = this.BooksService.get(1,
44-
// success => {
45-
// book1.attributes.title += ' :)'; // update view
46-
// console.log('book1', (<IDataResource>book1.relationships.author.data).attributes);
47-
// });
52+
console.log('BookRequest#3 requested');
53+
let book1 = this.BooksService.get(1,
54+
success => {
55+
console.log('BookRequest#3 received (author data from cache)',
56+
(<IDataResource>book1.relationships.author.data).attributes
57+
);
58+
});
4859
},
4960
error => {
5061
console.log('error books controller', error);
5162
}
5263
);
5364
}
5465

55-
public do() {
56-
this.BooksService.all();
57-
}
58-
5966
public delete(book: Jsonapi.IResource) {
6067
this.BooksService.delete(book.id);
6168
}

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 ng-click="$ctrl.do()">Books</h3>
4+
<h3>Books</h3>
55
<table class="table table-striped">
66
<thead>
77
<tr>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { IParamsCollection } from '../interfaces';
2+
3+
export interface IExecParams {
4+
id: string | null;
5+
params?: IParamsCollection | Function;
6+
fc_success?: Function;
7+
fc_error?: Function;
8+
exec_type: string;
9+
}

src/library/interfaces/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ export * from './params-collection.d'
1111
export * from './params-resource.d'
1212
export * from './resources-by-id.d'
1313
export * from './resources-by-type.d'
14+
export * from './exec-params.d'

src/library/interfaces/resource.d.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,14 @@ export interface IResource extends IDataResource {
77
lastupdate?: number;
88

99
type: string; // dont work extend?
10-
1110
relationships: IRelationships; // redefined from IDataResource
1211

13-
// new? (): IResource;
1412
reset? (): void;
1513
addRelationship? (resource: IResource, type_alias?: string): void;
1614
addRelationships? (resources: ICollection, type_alias: string): void;
1715
removeRelationship? (type_alias: string, id: string): boolean;
1816
addRelationshipsArray <T extends IResource>(resources: Array<T>, type_alias?: string): void;
1917
save? (params?: IParamsResource, fc_success?: Function, fc_error?: Function): any;
2018
toObject? (params?: IParamsResource): IDataObject;
21-
2219
getService(): IService;
2320
}

src/library/interfaces/service.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export interface IService {
99
delete (id: String, params?: IParamsResource | Function, success?: Function, error?: Function): void;
1010
getService? (): any; // any, becouse depends of extended class
1111
clearMemoryCache? (): boolean;
12-
1312
new? (): IResource;
1413
memorycache: ICache;
1514
}

src/library/resource.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Converter } from './services/resource-converter';
1010
// import { LocalFilter } from './services/localfilter';
1111
// import { MemoryCache } from './services/memorycache';
1212

13-
import { IAttributes, IResource, ICollection, IParamsResource } from './interfaces';
13+
import { IAttributes, IResource, ICollection, IExecParams, IParamsResource } from './interfaces';
1414
import { IRelationships, IRelationship } from './interfaces';
1515

1616
export class Resource extends ServiceWithRequests implements IResource {
@@ -105,18 +105,19 @@ export class Resource extends ServiceWithRequests implements IResource {
105105
}
106106

107107
public save<T extends IResource>(params?: Object | Function, fc_success?: Function, fc_error?: Function): Array<T> {
108-
return this.__exec(null, params, fc_success, fc_error, 'save');
108+
// return this.__exec(null, params, fc_success, fc_error, 'save');
109+
return this.__exec({ id: null, params: params, fc_success: fc_success, fc_error: fc_error, exec_type: 'save' });
109110
}
110111

111112
/**
112113
This method sort params for all(), get(), delete() and save()
113114
*/
114-
protected __exec(id: string, params: IParamsResource, fc_success, fc_error, exec_type: string): any {
115-
super.__exec(id, params, fc_success, fc_error, exec_type);
115+
protected __exec(exec_params: IExecParams): any {
116+
super.__exec(exec_params);
116117

117-
switch (exec_type) {
118+
switch (exec_params.exec_type) {
118119
case 'save':
119-
return this._save(params, fc_success, fc_error);
120+
return this._save(exec_params.params, exec_params.fc_success, exec_params.fc_error);
120121
}
121122
}
122123

src/library/service.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { Converter } from './services/resource-converter';
1111
import { LocalFilter } from './services/localfilter';
1212
import { MemoryCache } from './services/memorycache';
1313

14-
import { IService, ISchema, IResource, ICollection, ICache, IParamsCollection, IParamsResource } from './interfaces';
14+
import { IService, ISchema, IResource, ICollection, IExecParams, ICache, IParamsCollection, IParamsResource } from './interfaces';
1515
// import { IRelationships, IRelationship } from './interfaces';
1616

1717
export class Service extends ServiceWithRequests implements IService {
@@ -54,32 +54,34 @@ export class Service extends ServiceWithRequests implements IService {
5454
}
5555

5656
public get<T extends IResource>(id, params?: IParamsResource | Function, fc_success?: Function, fc_error?: Function): T {
57-
return this.__exec(id, params, fc_success, fc_error, 'get');
57+
// return this.__exec({ id, params, fc_success, fc_error, 'get' });
58+
return this.__exec({ id: id, params: params, fc_success: fc_success, fc_error: fc_error, exec_type: 'get' });
5859
}
5960

6061
public delete(id: string, params?: Object | Function, fc_success?: Function, fc_error?: Function): void {
61-
this.__exec(id, params, fc_success, fc_error, 'delete');
62+
// this.__exec(id, params, fc_success, fc_error, 'delete');
63+
return this.__exec({ id: id, params: params, fc_success: fc_success, fc_error: fc_error, exec_type: 'delete' });
6264
}
6365

6466
public all(params?: IParamsCollection | Function, fc_success?: Function, fc_error?: Function): ICollection {
65-
return this.__exec(null, params, fc_success, fc_error, 'all');
67+
return this.__exec({ id: null, params: params, fc_success: fc_success, fc_error: fc_error, exec_type: 'all' });
6668
}
6769

6870
/**
6971
This method sort params for all(), get(), delete() and save()
7072
*/
71-
protected __exec(id: string, params: IParamsResource, fc_success, fc_error, exec_type: string): any {
72-
super.__exec(id, params, fc_success, fc_error, exec_type);
73+
protected __exec(exec_params: IExecParams): any {
74+
super.__exec(exec_params);
7375

7476
this.schema = angular.extend({}, Base.Schema, this.schema);
7577

76-
switch (exec_type) {
78+
switch (exec_params.exec_type) {
7779
case 'get':
78-
return this._get(id, params, fc_success, fc_error);
80+
return this._get(exec_params.id, exec_params.params, exec_params.fc_success, exec_params.fc_error);
7981
case 'delete':
80-
return this._delete(id, params, fc_success, fc_error);
82+
return this._delete(exec_params.id, exec_params.params, exec_params.fc_success, exec_params.fc_error);
8183
case 'all':
82-
return this._all(params, fc_success, fc_error);
84+
return this._all(exec_params.params, exec_params.fc_success, exec_params.fc_error);
8385
}
8486
}
8587

src/library/services/memorycache.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,16 @@ export class MemoryCache implements ICache {
3131
public setCollection(url: string, collection: ICollection): void {
3232
// clone collection, because after maybe delete items for localfilter o pagination
3333
this.collections[url] = Base.newCollection();
34-
angular.forEach(collection, (value: IResource, key: string) => {
35-
this.collections[url][key] = value;
36-
this.setResource(value);
34+
angular.forEach(collection, (resource: IResource, resource_id: string) => {
35+
this.collections[url][resource_id] = resource;
36+
this.setResource(resource);
3737
});
3838
this.collections[url]['page'] = collection.page;
3939
this.collections_lastupdate[url] = Date.now();
4040
}
4141

4242
public setResource(resource: IResource): void {
43-
/*
44-
we cannot redefine object, because view don't update.
45-
*/
43+
// we cannot redefine object, because view don't update.
4644
if (resource.id in this.resources) {
4745
ResourceFunctions.resourceToResource(resource, this.resources[resource.id]);
4846
} else {

src/library/services/resource-functions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class ResourceFunctions {
2525
if ('id' in source.relationships[type_alias].data) {
2626
destination.addRelationship(
2727
(<IResource>source.relationships[type_alias].data),
28-
(<IResource>source.relationships[type_alias].data).type
28+
type_alias
2929
);
3030
} else {
3131
destination.addRelationships(<ICollection>source.relationships[type_alias].data, type_alias);

0 commit comments

Comments
 (0)