An open API service indexing awesome lists of open source software.

https://github.com/sgpinkus/angular-json-rest-service


https://github.com/sgpinkus/angular-json-rest-service

Last synced: 2 months ago
JSON representation

Awesome Lists containing this project

README

        

# Overview
This package provides a simple Angular 2 service `JsonRestService`, which is a wrapper of `@angular/http/Http`.

`JsonRestService` expects the remote HTTP service to be a ReST service taking and returning JSON with the usual semantics. `JsonRestService` will hydrate records it pulls off the wire into a user defined class implementing `ActiveRecord`. `Model` implements a very basic `ActiveRecord` you can use as a base and extend.

`JsonRestService` is generic like this: `JsonRestService`. In order to dynamically create instances of type `T` (for get(), and find()), the `JsonRestService` constructor also requires an instance of `ActiveRecordFactory`.

# Synopsis

import { Component } from '@angular/core';
import { Http } from '@angular/http';
import 'angular-json-rest-service/rxjs-extensions';
import { JsonRestService, ActiveRecordFactory } from 'angular-json-rest-service/json-rest.service';
// Base implementations of ActiveRecord / ActiveRecordFactory interfaces.
import { Model } from 'angular-json-rest-service/model';

class Hero extends Model {
name: string;
power(): number {
return 69;
}
}

class HeroFactory extends ActiveRecordFactory
{
create(object: Object, storage: JsonRestService): Hero {
var model = new Hero(storage)
model.merge(object)
return model;
}
}

@Component({
selector: 'my-app',
template: `

Hello {{hero.name}}. Your power is {{hero.power()}}

`
})
export class AppComponent {
hero: Hero;
constructor(private http:Http) {
var myService = new JsonRestService(
http,
'/root/heroes',
new HeroFactory()
)
this.hero = new Hero(myService)
myService.find().toPromise().then((m: Hero[]) => {
this.hero = m[0];
console.log(m.map(n => n.toJson()))
})
}
}