Skip to content

Commit 2c8a798

Browse files
committed
docs: use InMemoryWebApiModule everywhere
closes angular#2137
1 parent 5c5c9ca commit 2c8a798

File tree

7 files changed

+66
-69
lines changed

7 files changed

+66
-69
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// #docregion
22
import { BrowserModule } from '@angular/platform-browser';
33
import { FormsModule } from '@angular/forms';
4-
import { XHRBackend } from '@angular/http';
5-
/* import { routing,
6-
appRoutingProviders } from './app.routing';*/
4+
import { HttpModule } from '@angular/http';
5+
6+
/* import { routing } from './app.routing';*/
77
import { LocationStrategy,
88
HashLocationStrategy } from '@angular/common';
99
import { NgModule } from '@angular/core';
1010

1111
import { HeroData } from './hero-data';
12-
import { InMemoryBackendService,
13-
SEED_DATA } from 'angular2-in-memory-web-api';
12+
import { InMemoryWebApiModule } from 'angular2-in-memory-web-api';
13+
1414

1515
import { AppComponent } from './app.component';
1616
import { HeroBioComponent } from './hero-bio.component';
@@ -32,47 +32,43 @@ import { ParentFinderComponent,
3232
BethComponent,
3333
BobComponent } from './parent-finder.component';
3434

35-
const DIRECTIVES = [
35+
const declarations = [
36+
AppComponent,
3637
HeroBiosComponent, HeroBiosAndContactsComponent, HeroBioComponent,
3738
HeroesBaseComponent, SortedHeroesComponent,
3839
HeroOfTheMonthComponent, HeroContactComponent,
3940
HighlightDirective,
4041
ParentFinderComponent,
41-
AppComponent
4242
];
4343

44-
const B_DIRECTIVES = [ BarryComponent, BethComponent, BobComponent ];
44+
const a_components = [AliceComponent, AlexComponent ];
45+
46+
const b_components = [ BarryComponent, BethComponent, BobComponent ];
4547

46-
// #docregion C_DIRECTIVES
47-
const C_DIRECTIVES = [
48+
const c_components = [
4849
CarolComponent, ChrisComponent, CraigComponent,
4950
CathyComponent
5051
];
51-
// #enddocregion C_DIRECTIVES
5252

53-
// #docregion bootstrap
5453
@NgModule({
5554
imports: [
5655
BrowserModule,
5756
FormsModule,
57+
HttpModule,
58+
InMemoryWebApiModule.forRoot(HeroData)
5859
// routing TODO: add routes
5960
],
60-
declarations: [ ...DIRECTIVES,
61-
...B_DIRECTIVES,
62-
...C_DIRECTIVES,
63-
AliceComponent,
64-
AlexComponent ],
61+
declarations: [
62+
declarations,
63+
a_components,
64+
b_components,
65+
c_components,
66+
],
6567
bootstrap: [ AppComponent ],
68+
// #docregion providers
6669
providers: [
67-
// appRoutingProviders, TODO: add routes
68-
{ provide: LocationStrategy, useClass: HashLocationStrategy },
69-
70-
{ provide: XHRBackend, useClass: InMemoryBackendService }, // in-mem server
71-
{ provide: SEED_DATA, useClass: HeroData } // in-mem server data
70+
{ provide: LocationStrategy, useClass: HashLocationStrategy }
7271
]
72+
// #enddocregion providers
7373
})
74-
export class AppModule {
75-
constructor() {
76-
}
77-
}
78-
// #enddocregion bootstraps
74+
export class AppModule { }

public/docs/_examples/server-communication/ts/app/app.module.1.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { AppComponent } from './app.component';
1414
JsonpModule
1515
],
1616
declarations: [ AppComponent ],
17-
bootstrap: [ AppComponent ]
17+
bootstrap: [ AppComponent ]
1818
})
1919
export class AppModule { }
2020

public/docs/_examples/server-communication/ts/app/app.module.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
import { NgModule } from '@angular/core';
33
import { BrowserModule } from '@angular/platform-browser';
44
import { FormsModule } from '@angular/forms';
5-
import { HttpModule, JsonpModule, XHRBackend } from '@angular/http';
5+
import { HttpModule, JsonpModule } from '@angular/http';
66

7-
import { InMemoryBackendService, SEED_DATA } from 'angular2-in-memory-web-api';
8-
import { HeroData } from './hero-data';
9-
import { AppComponent } from './app.component';
7+
import { InMemoryWebApiModule } from 'angular2-in-memory-web-api';
8+
import { HeroData } from './hero-data';
9+
10+
import { AppComponent } from './app.component';
1011

1112
import { HeroListComponent } from './toh/hero-list.component';
1213
import { HeroListPromiseComponent } from './toh/hero-list.component.promise';
@@ -19,11 +20,10 @@ import { WikiSmartComponent } from './wiki/wiki-smart.component';
1920
BrowserModule,
2021
FormsModule,
2122
HttpModule,
22-
JsonpModule
23-
],
24-
providers: [
25-
{ provide: XHRBackend, useClass: InMemoryBackendService }, // in-mem server
26-
{ provide: SEED_DATA, useClass: HeroData } // in-mem server data
23+
JsonpModule,
24+
// #docregion in-mem-web-api
25+
InMemoryWebApiModule.forRoot(HeroData)
26+
// #enddocregion in-mem-web-api
2727
],
2828
declarations: [
2929
AppComponent,

public/docs/_examples/server-communication/ts/app/hero-data.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// #docregion
2-
export class HeroData {
2+
import { InMemoryDbService } from 'angular2-in-memory-web-api';
3+
export class HeroData implements InMemoryDbService {
34
createDb() {
45
let heroes = [
56
{ id: '1', name: 'Windstorm' },

public/docs/_examples/style-guide/ts/app/main.ts

+8-10
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@ import { NgModule } from '@angular/core';
22
import { browserDynamicPlatform } from '@angular/platform-browser-dynamic';
33
import { BrowserModule } from '@angular/platform-browser';
44

5-
import { HttpModule,
6-
XHRBackend } from '@angular/http';
5+
import { HttpModule} from '@angular/http';
6+
import { InMemoryWebApiModule } from 'angular2-in-memory-web-api';
7+
78
import { RouterModule } from '@angular/router';
89

910
import { HashLocationStrategy,
1011
LocationStrategy } from '@angular/common';
1112

12-
import { InMemoryBackendService,
13-
SEED_DATA } from 'angular2-in-memory-web-api';
14-
1513
import 'rxjs/add/operator/map';
1614

1715
import { HeroData } from './hero-data';
@@ -49,6 +47,7 @@ const moduleMetadata = {
4947
imports: [
5048
BrowserModule,
5149
HttpModule,
50+
InMemoryWebApiModule.forRoot(HeroData),
5251

5352
s0101.AppModule,
5453
s0207.AppModule,
@@ -77,17 +76,16 @@ const moduleMetadata = {
7776
s0704.AppModule,
7877
s0901.AppModule,
7978

79+
8080
RouterModule.forRoot([
8181
{ path: '', redirectTo: '/01-01', pathMatch: 'full' }
8282
], {/* enableTracing: true */}),
8383
],
84-
declarations: [AppComponent],
8584
providers: [
86-
{ provide: LocationStrategy, useClass: HashLocationStrategy },
87-
{ provide: XHRBackend, useClass: InMemoryBackendService },
88-
{ provide: SEED_DATA, useClass: HeroData }
85+
{ provide: LocationStrategy, useClass: HashLocationStrategy }
8986
],
90-
bootstrap: [ AppComponent ]
87+
declarations: [ AppComponent ],
88+
bootstrap: [ AppComponent ]
9189
};
9290

9391
@NgModule(moduleMetadata)

public/docs/ts/latest/cookbook/dependency-injection.jade

+7-16
Original file line numberDiff line numberDiff line change
@@ -80,25 +80,16 @@ include ../_util-fns
8080
.l-main-section
8181
:marked
8282
## External module configuration
83-
We can register _certain_ module providers in the `NgModule` rather than in the root application component.
83+
We often register providers in the `NgModule` rather than in the root application component.
8484

85-
We'd do this when we expect to select or configure external modules that support our application
86-
but (a) aren't conceptually part of the application and (b) that we could change later without
87-
altering the essential logic of the application.
85+
We do this when (a) we expect the service to be injectable everywhere
86+
or (b) we must configure another application global service _before it starts_.
8887

89-
For example, we might configure the Component Router with different
90-
[location strategies](../guide/router.html#location-strategy) based on environmental factors.
91-
The choice of location strategy doesn't matter to the application itself.
88+
We see an example of the second case here, where we configure the Component Router with a non-default
89+
[location strategy](../guide/router.html#location-strategy) by listing its provider
90+
in the `providers` list of the `AppModule`.
9291

93-
We could sneak in a fake HTTP backend with sample data during development rather than
94-
allow http calls to a remote server (that might not yet exist).
95-
We'll switch to the real backend in production.
96-
The application shouldn't know or care one way or the other.
97-
98-
See both examples in the following `app.module.ts`
99-
where we list their service providers in the `providers` array of the `NgModule` (AppModule).
100-
101-
+makeExample('cb-dependency-injection/ts/app/app.module.ts','bootstrap','app/app.module.ts')(format='.')
92+
+makeExample('cb-dependency-injection/ts/app/app.module.ts','providers','app/app.module.ts (providers)')(format='.')
10293

10394
a(id="injectable")
10495
a(id="nested-dependencies")

public/docs/ts/latest/guide/server-communication.jade

+16-5
Original file line numberDiff line numberDiff line change
@@ -669,15 +669,26 @@ a#in-mem-web-api
669669
Ensure that the `HeroService` endpoint refers to the web API:
670670
+makeExample('server-communication/ts/app/toh/hero.service.ts', 'endpoint')(format=".")
671671
:marked
672-
Finally, we need to redirect client HTTP requests to the in-memory web API.
672+
Finally, redirect client HTTP requests to the in-memory web API.
673673
block redirect-to-web-api
674674
:marked
675-
This redirection is easy to configure because Angular's `http` service delegates the client/server communication tasks
675+
This redirection is easy to configure with the in-memory web API service module.
676+
by adding the `InMemoryWebApiModule` to the `AppModule.imports` list.
677+
At the same time, we calling its `forRoot` configuration method with the `HeroData` class.
678+
+makeExample('server-communication/ts/app/app.module.ts', 'in-mem-web-api')(format=".")
679+
:marked
680+
### How it works
681+
682+
Angular's `http` service delegates the client/server communication tasks
676683
to a helper service called the `XHRBackend`.
677684

678-
To enable our server simulation, we replace the default `XHRBackend` service with
679-
the in-memory web API service using standard Angular provider registration techniques.
680-
We initialize the in-memory web API with *seed data* from the mock hero dataset at the same time.
685+
Using standard Angular provider registration techniques, the `InMemoryWebApiModule`
686+
replaces the default `XHRBackend` service with its own in-memory alternative.
687+
The `forRoot` method initialize the in-memory web API with *seed data* from the mock hero dataset at the same time.
688+
.l-sub-section
689+
:marked
690+
The `forRoot` method name is a strong reminder that you should only call the `InMemoryWebApiModule` _once_
691+
while setting the metadata for the root `AppModule`. Don't call it again!.
681692
:marked
682693
Here is the revised (and final) version of <span ngio-ex>app/app.module.ts></span> demonstrating these steps.
683694

0 commit comments

Comments
 (0)