Skip to content

Commit

Permalink
Implement NgModule
Browse files Browse the repository at this point in the history
  • Loading branch information
ghidoz committed Sep 2, 2016
1 parent 1e567c5 commit 410b3b2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
37 changes: 32 additions & 5 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ To install this library, run:
$ npm install angular2-jsonapi --save
```

If you use [Angular-CLI](https://github.com/angular/angular-cli), add the package (and its dependencies) to `system-config.ts`:
**Note**: If you use [Angular-CLI](https://github.com/angular/angular-cli) with SystemJS, add the package (and its dependencies) to `system-config.ts`:
```typescript
const map: any = {
'underscore': 'vendor/underscore',
Expand All @@ -49,7 +49,27 @@ vendorNpmFiles: [
]
```

Add the library's `PROVIDERS` to your bootstrap dependences:
If you use the [Angular-CLI Webpack version](https://github.com/angular/angular-cli/blob/master/WEBPACK_UPDATE.md), just skip to the following configuration step.

Add the `JsonApiModule` to your app module imports:
```typescript
import { JsonApiModule } from 'angular2-jsonapi';

@NgModule({
imports: [
BrowserModule,
JsonApiModule
],
declarations: [
AppComponent
],
bootstrap: [AppComponent]
})
export class AppModule { }
```

If you are still using Angular2 RC4, you can just add the library's `PROVIDERS` to your bootstrap dependencies:

```typescript
import { PROVIDERS } from 'angular2-jsonapi';

Expand All @@ -65,6 +85,7 @@ bootstrap(AppComponent, [
Firstly, create your `Datastore` service:
- Extend the `JsonApiDatastore` class
- Decorate it with `@JsonApiDatastoreConfig`, set the `baseUrl` for your APIs and map your models
- Pass the `Http` depencency to the parent constructor.

```typescript
import { JsonApiDatastoreConfig, JsonApiDatastore } from 'angular2-jsonapi';
Expand All @@ -78,7 +99,13 @@ import { JsonApiDatastoreConfig, JsonApiDatastore } from 'angular2-jsonapi';
users: User
}
})
export class Datastore extends JsonApiDatastore { }
export class Datastore extends JsonApiDatastore {

constructor(http: Http) {
super(http);
}

}
```

Then set up your models:
Expand Down Expand Up @@ -174,15 +201,15 @@ You will get an array of objects where every mapped relationship will be automat
Use `findRecord()` to retrieve a record by its type and ID:

```typescript
this.datastore.findRecord(Post, 1).subscribe(
this.datastore.findRecord(Post, '1').subscribe(
(post: Post) => console.log(post)
);
```

You can also pass the options:

```typescript
this.datastore.findRecord(Post, 1, {
this.datastore.findRecord(Post, '1', {
include: 'comments'
}).subscribe(
(post: Post) => console.log(post)
Expand Down
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { NgModule } from '@angular/core';
import { HttpModule } from '@angular/http';
import { PROVIDERS } from './services';

export * from './decorators/has-many.decorator';
Expand All @@ -9,6 +11,8 @@ export * from './models/json-api.model';

export * from './services';

export default {
providers: [PROVIDERS]
};
@NgModule({
providers: [ PROVIDERS ],
exports: [ HttpModule ]
})
export class JsonApiModule { }

0 comments on commit 410b3b2

Please sign in to comment.