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

Commit 18a604f

Browse files
authored
Merge pull request #18 from reyesoft/feature/store_with_promise
Feature/store with promise
2 parents 64429fa + 1139796 commit 18a604f

25 files changed

+381
-263
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1+
# 0.7.x (planned)
2+
3+
- Interfaces are in `ng.jsonapi` now
4+
- Resources can be extended for personalized classes
5+
16
# 0.6.x
27

38
## Localstorage cache
49

510
- Save on localstore all data. When you request a resource or collection, first check memory. If its empty, read from store. If is empty, get the data from back-end.
11+
- HttpStorage deprecated: jsons were saved as sent by the server, now we save json with logic (saving ids and resources separately).
12+
- Service with `toServer()` and `fromServer()` functions. They execute before and after http request. Ideal for type conversions.
13+
- `JsonapiCore.duplicateResource(resouce, ...relationtypes)` return a duplication of resource. You can duplicate resources and, optionally, their relationships. (v0.6.16)
14+
15+
## No more declaration file .d.ts
16+
617
- typings and index.d.ts removed. We only use `import`
718

819
# 0.5.x

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

src/demo/authors/authors.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class AuthorsController {
55

66
/** @ngInject */
77
constructor(
8-
protected JsonapiCore,
8+
protected JsonapiCore: Jsonapi.ICore,
99
protected AuthorsService: Jsonapi.IService
1010
) {
1111
this.authors = AuthorsService.all(

src/demo/authors/authors.service.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,14 @@ export class AuthorsService extends Jsonapi.Service {
2222
},
2323
ttl: 10
2424
};
25+
26+
// executed before get data from server
27+
public parseFromServer(attributes): void {
28+
attributes.name = attributes.name + ' ♥';
29+
}
30+
31+
// executed before send to server
32+
public parseToServer(attributes): void {
33+
attributes.name = attributes.name.replace('♥', '').trim();
34+
}
2535
}

src/library/core.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as angular from 'angular';
22
import './services/core-services.service';
3-
import { ICore, IService } from './interfaces';
3+
import { ICore, IResource, ICollection, IService } from './interfaces';
44

55
export class Core implements ICore {
66
private resourceServices: Object = {};
@@ -48,6 +48,33 @@ export class Core implements ICore {
4848
Core.injectedServices.JsonapiStoreService.clearCache();
4949
return true;
5050
}
51+
52+
// just an helper
53+
public duplicateResource(resource: IResource, ...relations_alias_to_duplicate_too: Array<string>): IResource {
54+
let newresource = this.getResourceService(resource.type).new();
55+
angular.merge(newresource.attributes, resource.attributes);
56+
newresource.attributes.name = newresource.attributes.name + ' xXx';
57+
angular.forEach(resource.relationships, (relationship, alias) => {
58+
if ('id' in relationship.data) {
59+
// relation hasOne
60+
if (relations_alias_to_duplicate_too.indexOf(alias) > -1) {
61+
newresource.addRelationship(this.duplicateResource(<IResource>relationship.data), alias);
62+
} else {
63+
newresource.addRelationship(<IResource>relationship.data, alias);
64+
}
65+
} else {
66+
// relation hasMany
67+
if (relations_alias_to_duplicate_too.indexOf(alias) > -1) {
68+
angular.forEach(relationship.data, relationresource => {
69+
newresource.addRelationship(this.duplicateResource(relationresource), alias);
70+
});
71+
} else {
72+
newresource.addRelationships(<ICollection>relationship.data, alias);
73+
}
74+
}
75+
});
76+
return newresource;
77+
}
5178
}
5279

5380
angular.module('Jsonapi.services').service('JsonapiCore', Core);

src/library/interfaces/cache.d.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
11
import { ICollection, IResource } from '../interfaces';
22

33
export interface ICache {
4-
resources: Object;
5-
6-
isCollectionExist(url: string): boolean;
7-
isCollectionLive(url: string, ttl: number): boolean;
8-
getOrCreateCollection(url: string, use_store?: boolean): ICollection;
94
setCollection(url: string, collection: ICollection): void;
10-
clearAllCollections(): boolean;
11-
12-
isResourceLive(id: string, ttl: number): boolean;
13-
getOrCreateResource(type: string, id: string, use_store?: boolean): IResource;
14-
getResource(id: string): IResource;
15-
getResourceFromStore(resource: IResource): void;
165
setResource(resource: IResource): void;
17-
18-
removeResource(id: string): void;
6+
deprecateCollections(path_start_with: string): boolean;
197
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { ICollection, IResource } from '../interfaces';
2+
import { ICache } from '../interfaces/cache.d';
3+
4+
export interface ICacheMemory extends ICache {
5+
resources: { [id: string]: IResource };
6+
7+
getOrCreateCollection(url: string): ICollection;
8+
isCollectionExist(url: string): boolean;
9+
isCollectionLive(url: string, ttl: number): boolean;
10+
11+
isResourceLive(id: string, ttl: number): boolean;
12+
getOrCreateResource(type: string, id: string): IResource;
13+
14+
removeResource(id: string): void;
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { ICollection, IResource } from '../interfaces';
2+
import { ICache } from '../interfaces/cache.d';
3+
4+
export interface ICacheStore extends ICache {
5+
getResource(resource: IResource): ng.IPromise<object>;
6+
getCollectionFromStorePromise(url:string, collection: ICollection): ng.IPromise<ICollection>;
7+
}

src/library/interfaces/collection.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export interface ICollection extends Array<IResource> {
66
$length: number;
77
$toArray: Array<IResource>;
88
$is_loading: boolean;
9-
$source: 'new' | 'memory' | 'store' | 'server' | 'httpstorage';
9+
$source: 'new' | 'memory' | 'store' | 'server';
1010
$cache_last_update: number;
1111
data: Array<IDataResource>; // this need disapear is for datacollection
1212
page: IPage;

src/library/interfaces/core.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IService } from './index';
1+
import { IService, IResource } from './index';
22

33
export interface ICore {
44
// jsonapiServices: Object;
@@ -13,6 +13,7 @@ export interface ICore {
1313
getResourceService(type: string): IService;
1414
refreshLoadings(factor: number): void;
1515
clearCache(): void;
16+
duplicateResource(resource: IResource, ...relations_types: Array<string>): IResource;
1617

1718
// static
1819
me?: IService;

0 commit comments

Comments
 (0)