https://github.com/sgpinkus/angular-json-rest-service
https://github.com/sgpinkus/angular-json-rest-service
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/sgpinkus/angular-json-rest-service
- Owner: sgpinkus
- License: mit
- Created: 2016-11-27T08:23:09.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-11-28T04:01:55.000Z (over 8 years ago)
- Last Synced: 2025-03-23T17:16:30.473Z (3 months ago)
- Language: TypeScript
- Size: 12.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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()))
})
}
}