Skip to content

Commit 0024502

Browse files
authored
Services work like data models
* models done * models with relationships done * version bump * version bump
1 parent e2f6c0a commit 0024502

File tree

9 files changed

+82
-28
lines changed

9 files changed

+82
-28
lines changed

demo/app/authors/authors.service.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { Injectable } from '@angular/core';
2-
import { Service, ISchema } from 'ngx-jsonapi';
2+
import { Service, ISchema, Resource, ICollection } from 'ngx-jsonapi';
3+
import { Book } from '../books/books.service';
34

45
@Injectable()
5-
export class AuthorsService extends Service {
6+
export class AuthorsService extends Service<Author> {
7+
public resource = Author;
68
public type = 'authors';
79
public schema: ISchema = {
810
attributes: {
@@ -22,3 +24,25 @@ export class AuthorsService extends Service {
2224
}
2325
};
2426
}
27+
28+
export class Author extends Resource {
29+
public attributes: {
30+
name: string,
31+
date_of_birth: string,
32+
date_of_death: string,
33+
created_at: string,
34+
updated_at: string
35+
};
36+
37+
public getName() {
38+
return this.attributes.name;
39+
}
40+
41+
public books(): ICollection<Book> {
42+
return <ICollection<Book>>this.relationships.books.data;
43+
}
44+
45+
public photos()/*: ICollection<Photo>*/ {
46+
return <ICollection>this.relationships.photos.data;
47+
}
48+
}

demo/app/authors/components/author.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ <h4>Books</h4>
2626
<th>Date Published</th>
2727
</tr>
2828
</thead>
29-
<tr *ngFor="let book of author.relationships.books.data.$toArray">
29+
<tr *ngFor="let book of author.books().$toArray">
3030
<td>{{ book.id }}</td>
3131
<td>
3232
<a>{{ book.attributes.title }}</a>

demo/app/authors/components/author.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import { Resource, IRelationship, ICollection } from 'ngx-jsonapi';
44

55
import { forEach } from '../../../foreach';
66
import { PhotosService } from '../../photos/photos.service';
7-
import { AuthorsService } from '../authors.service';
7+
import { AuthorsService, Author } from '../authors.service';
88

99
@Component({
1010
selector: 'demo-author',
1111
templateUrl: './author.component.html'
1212
})
1313
export class AuthorComponent {
14-
public author: Resource;
14+
public author: Author;
1515
public relatedbooks: Array<Resource>;
1616

1717
public constructor(

demo/app/books/books.service.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Injectable } from '@angular/core';
2-
import { Service, ISchema } from 'ngx-jsonapi';
2+
import { Service, ISchema, Resource, ICollection } from 'ngx-jsonapi';
3+
import { Author } from '../authors/authors.service';
4+
import { Photo } from '../photos/photos.service';
35

46
@Injectable()
57
export class BooksService extends Service {
@@ -33,3 +35,20 @@ export class BooksService extends Service {
3335
}
3436
}
3537
}
38+
39+
export class Book extends Resource {
40+
public attributes: {
41+
date_published: { },
42+
title: { presence: true, length: { maximum: 96 } },
43+
created_at: { },
44+
updated_at: { }
45+
};
46+
47+
public author(): Author {
48+
return <Author>this.relationships.authors.data;
49+
}
50+
51+
public photos(): ICollection<Photo> {
52+
return <ICollection<Photo>>this.relationships.photos.data;
53+
}
54+
}

demo/app/photos/photos.service.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Injectable } from '@angular/core';
2-
import { Service, ISchema } from 'ngx-jsonapi';
2+
import { Service, ISchema, Resource } from 'ngx-jsonapi';
33

44
@Injectable()
55
export class PhotosService extends Service {
@@ -14,3 +14,13 @@ export class PhotosService extends Service {
1414
}
1515
};
1616
}
17+
18+
export class Photo extends Resource {
19+
public attributes: {
20+
title: {},
21+
uri: {},
22+
imageable_id: {},
23+
created_at: {},
24+
updated_at: {}
25+
};
26+
}

src/core.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ export class Core {
4646
};
4747
}
4848

49-
public registerService(clase: Service): Service | false {
49+
public registerService<R extends Resource>(clase: Service): Service<R> | false {
5050
if (clase.type in this.resourceServices) {
5151
return false;
5252
}
5353
this.resourceServices[clase.type] = clase;
5454

55-
return clase;
55+
return <Service<R>>clase;
5656
}
5757

5858
public getResourceService(type: string): Service {

src/interfaces/collection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { Resource } from '../resource';
22
import { IPage } from './page';
33
import { IDataResource } from './data-resource';
44

5-
export interface ICollection extends Array<Resource> {
5+
export interface ICollection<R extends Resource = Resource> extends Array<Resource> {
66
$length: number;
7-
$toArray: Array<Resource>;
7+
$toArray: Array<R>;
88
$is_loading: boolean;
99
$source: 'new' | 'memory' | 'store' | 'server';
1010
$cache_last_update: number;

src/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-jsonapi",
3-
"version": "0.1.0-rc.3",
3+
"version": "0.2.0",
44
"description": "JSON API library for Angular",
55
"module": "ngx-jsonapi/@ngx-jsonapi/ngx-jsonapi.es5.js",
66
"es2015": "ngx-jsonapi/@ngx-jsonapi/ngx-jsonapi.js",

src/service.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ import {
2020
IAttributes,
2121
} from './interfaces';
2222

23-
export class Service extends ParentResourceService {
23+
export class Service<R extends Resource = Resource> extends ParentResourceService {
2424
public schema: ISchema;
2525
public cachememory: ICacheMemory;
2626
public cachestore: CacheStore;
2727
public type: string;
28+
public resource = Resource;
2829

2930
private path: string; // without slashes
3031
private smartfiltertype = 'undefined';
@@ -33,7 +34,7 @@ export class Service extends ParentResourceService {
3334
Register schema on Core
3435
@return true if the resource don't exist and registered ok
3536
*/
36-
public register(): Service | false {
37+
public register(): Service<R> | false {
3738
if (Core.me === null) {
3839
throw new Error(
3940
'Error: you are trying register `' +
@@ -46,21 +47,21 @@ export class Service extends ParentResourceService {
4647
this.cachestore = new CacheStore();
4748
this.schema = { ...{}, ...Base.Schema, ...this.schema };
4849

49-
return Core.me.registerService(this);
50+
return Core.me.registerService<R>(this);
5051
}
5152

52-
public newResource(): Resource {
53-
let resource: Resource = new Resource();
53+
public newResource(): R {
54+
let resource = new this.resource();
5455

55-
return resource;
56+
return <R>resource;
5657
}
5758

58-
public new<T extends Resource>(): T {
59+
public new(): R {
5960
let resource = this.newResource();
6061
resource.type = this.type;
6162
resource.reset();
6263

63-
return <T>resource;
64+
return resource;
6465
}
6566

6667
public getPrePath(): string {
@@ -70,13 +71,13 @@ export class Service extends ParentResourceService {
7071
return this.path ? this.path : this.type;
7172
}
7273

73-
public get<T extends Resource>(
74+
public get(
7475
id: string,
7576
params?: IParamsResource | Function,
7677
fc_success?: Function,
7778
fc_error?: Function
78-
): T {
79-
return <T>this.__exec({
79+
): R {
80+
return <R>this.__exec({
8081
id: id,
8182
params: params,
8283
fc_success: fc_success,
@@ -114,7 +115,7 @@ export class Service extends ParentResourceService {
114115
});
115116
}
116117

117-
protected __exec(exec_params: IExecParams): Resource | ICollection | void {
118+
protected __exec(exec_params: IExecParams): R | ICollection | void {
118119
let exec_pp = super.proccess_exec_params(exec_params);
119120

120121
switch (exec_pp.exec_type) {
@@ -146,7 +147,7 @@ export class Service extends ParentResourceService {
146147
params: IParamsResource,
147148
fc_success,
148149
fc_error
149-
): Resource {
150+
): R {
150151
// http request
151152
let path = new PathBuilder();
152153
path.applyParams(this, params);
@@ -175,7 +176,7 @@ export class Service extends ParentResourceService {
175176
}
176177
);
177178

178-
return resource;
179+
return <R>resource;
179180
} else if (Core.injectedServices.rsJsonapiConfig.cachestore_support) {
180181
// CACHESTORE
181182
this.getService()
@@ -199,7 +200,7 @@ export class Service extends ParentResourceService {
199200
this.getGetFromServer(path, fc_success, fc_error, resource);
200201
}
201202

202-
return resource;
203+
return <R>resource;
203204
}
204205

205206
private getGetFromServer(path, fc_success, fc_error, resource: Resource) {
@@ -477,7 +478,7 @@ export class Service extends ParentResourceService {
477478
/*
478479
@return This resource like a service
479480
*/
480-
public getService<T extends Service>(): T {
481+
public getService<T extends Service<R>>(): T {
481482
return <T>(Converter.getService(this.type) || this.register());
482483
// let serv = Converter.getService(this.type);
483484
// if (serv) {

0 commit comments

Comments
 (0)