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

Commit c24c46a

Browse files
committed
small fixes
1 parent 0c0bc67 commit c24c46a

6 files changed

Lines changed: 25 additions & 52 deletions

File tree

src/library/interfaces/cache.d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ICollection } from '../interfaces';
1+
import { ICollection, IResource } from '../interfaces';
22

33
export interface ICache {
44
// collections: Object;
@@ -8,7 +8,10 @@ export interface ICache {
88
isCollectionLive(url: string, ttl: number): boolean;
99
getCollection(url: string): ICollection;
1010
setCollection(url: string, collection: ICollection): void;
11-
clearAllCollections(): void;
11+
clearAllCollections(): boolean;
12+
isResourceLive(id: string, ttl: number): boolean;
13+
setResource(resource: IResource): void;
14+
getCollection(url: string): ICollection;
1215

1316
removeResource(id: string): void;
1417
}

src/library/interfaces/service.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { ISchema, IResource, ICollection, IParamsCollection, ICache, IParamsReso
33
export interface IService {
44
smartfilter?: string;
55
schema?: ISchema;
6+
getPrePath(): string;
7+
getPath(): string;
68
register? (): boolean;
79
get<T extends IResource>(id: string | number, params?: IParamsResource | Function, fc_success?: Function, fc_error?: Function): T;
810
all(params?: IParamsCollection | Function, success?: Function, error?: Function): ICollection;

src/library/resource.ts

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,21 @@ import { PathBuilder } from './services/path-builder';
88
// import { UrlParamsBuilder } from './services/url-params-builder';
99
import { Converter } from './services/resource-converter';
1010

11-
import { IAttributes, IResource, ICollection, IExecParams, IParamsResource } from './interfaces';
11+
import { IService, IAttributes, IResource, ICollection, IExecParams, IParamsResource } from './interfaces';
1212
import { IRelationships, IRelationship } from './interfaces';
1313

1414
export class Resource extends ParentResourceService implements IResource {
1515
public is_new = true;
1616
public is_loading = false;
1717
public is_saving = false;
18-
1918
public id: string = '';
2019
public type: string = '';
2120
public attributes: IAttributes = {};
2221
public relationships: IRelationships = {};
2322

24-
public reset(): void {
25-
this.id = '';
26-
this.attributes = {};
27-
angular.forEach(this.getService().schema.attributes, (value, key) => {
28-
this.attributes[key] = ('default' in value) ? value.default : undefined;
29-
});
30-
this.relationships = {};
31-
angular.forEach(this.getService().schema.relationships, (value, key) => {
32-
this.relationships[key] = <IRelationship>{ data: {} };
33-
if (this.getService().schema.relationships[key].hasMany) {
34-
this.relationships[key].data = Base.newCollection();
35-
}
36-
});
37-
this.is_new = true;
38-
}
39-
4023
public toObject(params?: IParamsResource): IDataObject {
4124
params = angular.extend({}, Base.Params, params);
42-
this.getService().schema = angular.extend({}, Base.Schema, this.getService().schema);
25+
// this.getService().schema = angular.extend({}, Base.Schema, this.getService().schema);
4326

4427
let relationships = { };
4528
let included = [ ];
@@ -103,13 +86,9 @@ export class Resource extends ParentResourceService implements IResource {
10386
}
10487

10588
public save<T extends IResource>(params?: Object | Function, fc_success?: Function, fc_error?: Function): Array<T> {
106-
// return this.__exec(null, params, fc_success, fc_error, 'save');
10789
return this.__exec({ id: null, params: params, fc_success: fc_success, fc_error: fc_error, exec_type: 'save' });
10890
}
10991

110-
/**
111-
This method sort params for all(), get(), delete() and save()
112-
*/
11392
protected __exec(exec_params: IExecParams): any {
11493
super.__exec(exec_params);
11594

@@ -247,7 +226,7 @@ export class Resource extends ParentResourceService implements IResource {
247226
/**
248227
@return This resource like a service
249228
**/
250-
public getService() {
229+
public getService(): IService {
251230
return Converter.getService(this.type);
252231
}
253232
}

src/library/service.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ import { MemoryCache } from './services/memorycache';
1414
import { IService, ISchema, IResource, ICollection, IExecParams, ICache, IParamsCollection, IParamsResource } from './interfaces';
1515

1616
export class Service extends ParentResourceService implements IService {
17-
public is_new = true;
18-
public is_loading = false;
19-
public is_saving = false;
2017
public schema: ISchema;
2118
public memorycache: ICache;
2219
public type: string;
@@ -34,6 +31,7 @@ export class Service extends ParentResourceService implements IService {
3431
}
3532
// only when service is registered, not cloned object
3633
this.memorycache = new MemoryCache();
34+
this.schema = angular.extend({}, Base.Schema, this.schema);
3735
return Core.Me._register(this);
3836
}
3937

@@ -53,27 +51,20 @@ export class Service extends ParentResourceService implements IService {
5351
}
5452

5553
public get<T extends IResource>(id, params?: IParamsResource | Function, fc_success?: Function, fc_error?: Function): T {
56-
// return this.__exec({ id, params, fc_success, fc_error, 'get' });
5754
return this.__exec({ id: id, params: params, fc_success: fc_success, fc_error: fc_error, exec_type: 'get' });
5855
}
5956

6057
public delete(id: string, params?: Object | Function, fc_success?: Function, fc_error?: Function): void {
61-
// this.__exec(id, params, fc_success, fc_error, 'delete');
6258
return this.__exec({ id: id, params: params, fc_success: fc_success, fc_error: fc_error, exec_type: 'delete' });
6359
}
6460

6561
public all(params?: IParamsCollection | Function, fc_success?: Function, fc_error?: Function): ICollection {
6662
return this.__exec({ id: null, params: params, fc_success: fc_success, fc_error: fc_error, exec_type: 'all' });
6763
}
6864

69-
/**
70-
This method sort params for all(), get(), delete() and save()
71-
*/
7265
protected __exec(exec_params: IExecParams): any {
7366
super.__exec(exec_params);
7467

75-
this.schema = angular.extend({}, Base.Schema, this.schema);
76-
7768
switch (exec_params.exec_type) {
7869
case 'get':
7970
return this._get(exec_params.id, exec_params.params, exec_params.fc_success, exec_params.fc_error);
@@ -265,8 +256,6 @@ export class Service extends ParentResourceService implements IService {
265256
.delete(path.get())
266257
.then(
267258
success => {
268-
// we don't use more temporary_collection
269-
// delete this.tempororay_collection[id];
270259
this.getService().memorycache.removeResource(id);
271260
this.runFc(fc_success, success);
272261
},
@@ -279,11 +268,11 @@ export class Service extends ParentResourceService implements IService {
279268
/**
280269
@return This resource like a service
281270
**/
282-
public getService() {
271+
public getService(): IService {
283272
return Converter.getService(this.type);
284273
}
285274

286-
public clearMemoryCache() {
275+
public clearMemoryCache(): boolean {
287276
return this.getService().memorycache.clearAllCollections();
288277
}
289278
}

src/library/services/memorycache.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ export class MemoryCache implements ICache {
88
private collections_lastupdate = {};
99
public resources = {};
1010

11-
public isCollectionExist(url: string): boolean {
11+
public isCollectionExist(url: string): boolean {
1212
return (url in this.collections && this.collections[url].$source !== 'new' ? true : false);
1313
}
1414

15-
public isCollectionLive(url: string, ttl: number): boolean {
15+
public isCollectionLive(url: string, ttl: number): boolean {
1616
return (Date.now() <= (this.collections_lastupdate[url] + ttl * 1000));
1717
}
1818

19-
public isResourceLive(id: string, ttl: number): boolean {
19+
public isResourceLive(id: string, ttl: number): boolean {
2020
return this.resources[id] && (Date.now() <= (this.resources[id].lastupdate + ttl * 1000));
2121
}
2222

23-
public getCollection(url: string): ICollection {
23+
public getCollection(url: string): ICollection {
2424
if (!(url in this.collections)) {
2525
this.collections[url] = Base.newCollection();
2626
this.collections[url].$source = 'new';

src/library/services/path-builder.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
export class PathBuilder {
2-
public paths: Array<String> = [];
3-
public includes: Array<String> = [];
4-
private get_params: Array<String> = [];
2+
public paths: Array<string> = [];
3+
public includes: Array<string> = [];
4+
private get_params: Array<string> = [];
55

6-
public prependPath(value: String) {
6+
public prependPath(value: string) {
77
this.paths.unshift(value);
88
}
99

10-
public appendPath(value: String) {
10+
public appendPath(value: string) {
1111
if (value !== '') {
1212
this.paths.push(value);
1313
}
@@ -17,15 +17,15 @@ export class PathBuilder {
1717
this.get_params.push(param);
1818
}
1919

20-
public setInclude(strings_array: Array<String>) {
20+
public setInclude(strings_array: Array<string>) {
2121
this.includes = strings_array;
2222
}
2323

24-
public getForCache(): String {
24+
public getForCache(): string {
2525
return this.paths.join('/') + this.get_params.join('/');
2626
}
2727

28-
public get(): String {
28+
public get(): string {
2929
var params = [];
3030
angular.copy(this.get_params, params);
3131

0 commit comments

Comments
 (0)