Skip to content

Commit

Permalink
Services work like data models
Browse files Browse the repository at this point in the history
* models done

* models with relationships done

* version bump

* version bump
  • Loading branch information
pablorsk authored Dec 27, 2017
1 parent e2f6c0a commit 0024502
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 28 deletions.
28 changes: 26 additions & 2 deletions demo/app/authors/authors.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Injectable } from '@angular/core';
import { Service, ISchema } from 'ngx-jsonapi';
import { Service, ISchema, Resource, ICollection } from 'ngx-jsonapi';
import { Book } from '../books/books.service';

@Injectable()
export class AuthorsService extends Service {
export class AuthorsService extends Service<Author> {
public resource = Author;
public type = 'authors';
public schema: ISchema = {
attributes: {
Expand All @@ -22,3 +24,25 @@ export class AuthorsService extends Service {
}
};
}

export class Author extends Resource {
public attributes: {
name: string,
date_of_birth: string,
date_of_death: string,
created_at: string,
updated_at: string
};

public getName() {
return this.attributes.name;
}

public books(): ICollection<Book> {
return <ICollection<Book>>this.relationships.books.data;
}

public photos()/*: ICollection<Photo>*/ {
return <ICollection>this.relationships.photos.data;
}
}
2 changes: 1 addition & 1 deletion demo/app/authors/components/author.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ <h4>Books</h4>
<th>Date Published</th>
</tr>
</thead>
<tr *ngFor="let book of author.relationships.books.data.$toArray">
<tr *ngFor="let book of author.books().$toArray">
<td>{{ book.id }}</td>
<td>
<a>{{ book.attributes.title }}</a>
Expand Down
4 changes: 2 additions & 2 deletions demo/app/authors/components/author.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { Resource, IRelationship, ICollection } from 'ngx-jsonapi';

import { forEach } from '../../../foreach';
import { PhotosService } from '../../photos/photos.service';
import { AuthorsService } from '../authors.service';
import { AuthorsService, Author } from '../authors.service';

@Component({
selector: 'demo-author',
templateUrl: './author.component.html'
})
export class AuthorComponent {
public author: Resource;
public author: Author;
public relatedbooks: Array<Resource>;

public constructor(
Expand Down
21 changes: 20 additions & 1 deletion demo/app/books/books.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Injectable } from '@angular/core';
import { Service, ISchema } from 'ngx-jsonapi';
import { Service, ISchema, Resource, ICollection } from 'ngx-jsonapi';
import { Author } from '../authors/authors.service';
import { Photo } from '../photos/photos.service';

@Injectable()
export class BooksService extends Service {
Expand Down Expand Up @@ -33,3 +35,20 @@ export class BooksService extends Service {
}
}
}

export class Book extends Resource {
public attributes: {
date_published: { },
title: { presence: true, length: { maximum: 96 } },
created_at: { },
updated_at: { }
};

public author(): Author {
return <Author>this.relationships.authors.data;
}

public photos(): ICollection<Photo> {
return <ICollection<Photo>>this.relationships.photos.data;
}
}
12 changes: 11 additions & 1 deletion demo/app/photos/photos.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@angular/core';
import { Service, ISchema } from 'ngx-jsonapi';
import { Service, ISchema, Resource } from 'ngx-jsonapi';

@Injectable()
export class PhotosService extends Service {
Expand All @@ -14,3 +14,13 @@ export class PhotosService extends Service {
}
};
}

export class Photo extends Resource {
public attributes: {
title: {},
uri: {},
imageable_id: {},
created_at: {},
updated_at: {}
};
}
4 changes: 2 additions & 2 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ export class Core {
};
}

public registerService(clase: Service): Service | false {
public registerService<R extends Resource>(clase: Service): Service<R> | false {
if (clase.type in this.resourceServices) {
return false;
}
this.resourceServices[clase.type] = clase;

return clase;
return <Service<R>>clase;
}

public getResourceService(type: string): Service {
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { Resource } from '../resource';
import { IPage } from './page';
import { IDataResource } from './data-resource';

export interface ICollection extends Array<Resource> {
export interface ICollection<R extends Resource = Resource> extends Array<Resource> {
$length: number;
$toArray: Array<Resource>;
$toArray: Array<R>;
$is_loading: boolean;
$source: 'new' | 'memory' | 'store' | 'server';
$cache_last_update: number;
Expand Down
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngx-jsonapi",
"version": "0.1.0-rc.3",
"version": "0.2.0",
"description": "JSON API library for Angular",
"module": "ngx-jsonapi/@ngx-jsonapi/ngx-jsonapi.es5.js",
"es2015": "ngx-jsonapi/@ngx-jsonapi/ngx-jsonapi.js",
Expand Down
33 changes: 17 additions & 16 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ import {
IAttributes,
} from './interfaces';

export class Service extends ParentResourceService {
export class Service<R extends Resource = Resource> extends ParentResourceService {
public schema: ISchema;
public cachememory: ICacheMemory;
public cachestore: CacheStore;
public type: string;
public resource = Resource;

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

return Core.me.registerService(this);
return Core.me.registerService<R>(this);
}

public newResource(): Resource {
let resource: Resource = new Resource();
public newResource(): R {
let resource = new this.resource();

return resource;
return <R>resource;
}

public new<T extends Resource>(): T {
public new(): R {
let resource = this.newResource();
resource.type = this.type;
resource.reset();

return <T>resource;
return resource;
}

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

public get<T extends Resource>(
public get(
id: string,
params?: IParamsResource | Function,
fc_success?: Function,
fc_error?: Function
): T {
return <T>this.__exec({
): R {
return <R>this.__exec({
id: id,
params: params,
fc_success: fc_success,
Expand Down Expand Up @@ -114,7 +115,7 @@ export class Service extends ParentResourceService {
});
}

protected __exec(exec_params: IExecParams): Resource | ICollection | void {
protected __exec(exec_params: IExecParams): R | ICollection | void {
let exec_pp = super.proccess_exec_params(exec_params);

switch (exec_pp.exec_type) {
Expand Down Expand Up @@ -146,7 +147,7 @@ export class Service extends ParentResourceService {
params: IParamsResource,
fc_success,
fc_error
): Resource {
): R {
// http request
let path = new PathBuilder();
path.applyParams(this, params);
Expand Down Expand Up @@ -175,7 +176,7 @@ export class Service extends ParentResourceService {
}
);

return resource;
return <R>resource;
} else if (Core.injectedServices.rsJsonapiConfig.cachestore_support) {
// CACHESTORE
this.getService()
Expand All @@ -199,7 +200,7 @@ export class Service extends ParentResourceService {
this.getGetFromServer(path, fc_success, fc_error, resource);
}

return resource;
return <R>resource;
}

private getGetFromServer(path, fc_success, fc_error, resource: Resource) {
Expand Down Expand Up @@ -477,7 +478,7 @@ export class Service extends ParentResourceService {
/*
@return This resource like a service
*/
public getService<T extends Service>(): T {
public getService<T extends Service<R>>(): T {
return <T>(Converter.getService(this.type) || this.register());
// let serv = Converter.getService(this.type);
// if (serv) {
Expand Down

0 comments on commit 0024502

Please sign in to comment.