From 0024502652de677bc945f1431dd4df80669fb6a4 Mon Sep 17 00:00:00 2001 From: Pablo Reyes Date: Wed, 27 Dec 2017 08:31:15 -0300 Subject: [PATCH] Services work like data models * models done * models with relationships done * version bump * version bump --- demo/app/authors/authors.service.ts | 28 ++++++++++++++-- .../authors/components/author.component.html | 2 +- .../authors/components/author.component.ts | 4 +-- demo/app/books/books.service.ts | 21 +++++++++++- demo/app/photos/photos.service.ts | 12 ++++++- src/core.ts | 4 +-- src/interfaces/collection.ts | 4 +-- src/package.json | 2 +- src/service.ts | 33 ++++++++++--------- 9 files changed, 82 insertions(+), 28 deletions(-) diff --git a/demo/app/authors/authors.service.ts b/demo/app/authors/authors.service.ts index 75b36c02..82becb0a 100644 --- a/demo/app/authors/authors.service.ts +++ b/demo/app/authors/authors.service.ts @@ -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 { + public resource = Author; public type = 'authors'; public schema: ISchema = { attributes: { @@ -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 { + return >this.relationships.books.data; + } + + public photos()/*: ICollection*/ { + return this.relationships.photos.data; + } +} diff --git a/demo/app/authors/components/author.component.html b/demo/app/authors/components/author.component.html index 6df34ffd..33502559 100644 --- a/demo/app/authors/components/author.component.html +++ b/demo/app/authors/components/author.component.html @@ -26,7 +26,7 @@

Books

Date Published - + {{ book.id }} {{ book.attributes.title }} diff --git a/demo/app/authors/components/author.component.ts b/demo/app/authors/components/author.component.ts index 9851d103..4c9710ed 100644 --- a/demo/app/authors/components/author.component.ts +++ b/demo/app/authors/components/author.component.ts @@ -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; public constructor( diff --git a/demo/app/books/books.service.ts b/demo/app/books/books.service.ts index a1b127ff..c8578ddc 100644 --- a/demo/app/books/books.service.ts +++ b/demo/app/books/books.service.ts @@ -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 { @@ -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 this.relationships.authors.data; + } + + public photos(): ICollection { + return >this.relationships.photos.data; + } +} diff --git a/demo/app/photos/photos.service.ts b/demo/app/photos/photos.service.ts index 77de28f7..0041f9ae 100644 --- a/demo/app/photos/photos.service.ts +++ b/demo/app/photos/photos.service.ts @@ -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 { @@ -14,3 +14,13 @@ export class PhotosService extends Service { } }; } + +export class Photo extends Resource { + public attributes: { + title: {}, + uri: {}, + imageable_id: {}, + created_at: {}, + updated_at: {} + }; +} diff --git a/src/core.ts b/src/core.ts index a70e3931..3f84771d 100644 --- a/src/core.ts +++ b/src/core.ts @@ -46,13 +46,13 @@ export class Core { }; } - public registerService(clase: Service): Service | false { + public registerService(clase: Service): Service | false { if (clase.type in this.resourceServices) { return false; } this.resourceServices[clase.type] = clase; - return clase; + return >clase; } public getResourceService(type: string): Service { diff --git a/src/interfaces/collection.ts b/src/interfaces/collection.ts index f1940a8a..5f6f37ea 100644 --- a/src/interfaces/collection.ts +++ b/src/interfaces/collection.ts @@ -2,9 +2,9 @@ import { Resource } from '../resource'; import { IPage } from './page'; import { IDataResource } from './data-resource'; -export interface ICollection extends Array { +export interface ICollection extends Array { $length: number; - $toArray: Array; + $toArray: Array; $is_loading: boolean; $source: 'new' | 'memory' | 'store' | 'server'; $cache_last_update: number; diff --git a/src/package.json b/src/package.json index a358e7d5..abe90716 100644 --- a/src/package.json +++ b/src/package.json @@ -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", diff --git a/src/service.ts b/src/service.ts index ca28e741..c4c55c00 100644 --- a/src/service.ts +++ b/src/service.ts @@ -20,11 +20,12 @@ import { IAttributes, } from './interfaces'; -export class Service extends ParentResourceService { +export class Service 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'; @@ -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 | false { if (Core.me === null) { throw new Error( 'Error: you are trying register `' + @@ -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(this); } - public newResource(): Resource { - let resource: Resource = new Resource(); + public newResource(): R { + let resource = new this.resource(); - return resource; + return resource; } - public new(): T { + public new(): R { let resource = this.newResource(); resource.type = this.type; resource.reset(); - return resource; + return resource; } public getPrePath(): string { @@ -70,13 +71,13 @@ export class Service extends ParentResourceService { return this.path ? this.path : this.type; } - public get( + public get( id: string, params?: IParamsResource | Function, fc_success?: Function, fc_error?: Function - ): T { - return this.__exec({ + ): R { + return this.__exec({ id: id, params: params, fc_success: fc_success, @@ -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) { @@ -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); @@ -175,7 +176,7 @@ export class Service extends ParentResourceService { } ); - return resource; + return resource; } else if (Core.injectedServices.rsJsonapiConfig.cachestore_support) { // CACHESTORE this.getService() @@ -199,7 +200,7 @@ export class Service extends ParentResourceService { this.getGetFromServer(path, fc_success, fc_error, resource); } - return resource; + return resource; } private getGetFromServer(path, fc_success, fc_error, resource: Resource) { @@ -477,7 +478,7 @@ export class Service extends ParentResourceService { /* @return This resource like a service */ - public getService(): T { + public getService>(): T { return (Converter.getService(this.type) || this.register()); // let serv = Converter.getService(this.type); // if (serv) {