docs(HTTP Client): edit copy to conform to G-doc guidelines. (#2630)
This commit is contained in:
parent
2f5306f1b6
commit
c28b003a3c
@ -3,7 +3,7 @@
|
|||||||
import { Component } from '@angular/core';
|
import { Component } from '@angular/core';
|
||||||
|
|
||||||
// #docregion import-rxjs
|
// #docregion import-rxjs
|
||||||
// Add the RxJS Observable operators we need in this app.
|
// Add the RxJS Observable operators.
|
||||||
import './rxjs-operators';
|
import './rxjs-operators';
|
||||||
// #enddocregion import-rxjs
|
// #enddocregion import-rxjs
|
||||||
|
|
||||||
|
@ -3,10 +3,10 @@ import { InMemoryDbService } from 'angular-in-memory-web-api';
|
|||||||
export class HeroData implements InMemoryDbService {
|
export class HeroData implements InMemoryDbService {
|
||||||
createDb() {
|
createDb() {
|
||||||
let heroes = [
|
let heroes = [
|
||||||
{ id: '1', name: 'Windstorm' },
|
{ id: 1, name: 'Windstorm' },
|
||||||
{ id: '2', name: 'Bombasto' },
|
{ id: 2, name: 'Bombasto' },
|
||||||
{ id: '3', name: 'Magneta' },
|
{ id: 3, name: 'Magneta' },
|
||||||
{ id: '4', name: 'Tornado' }
|
{ id: 4, name: 'Tornado' }
|
||||||
];
|
];
|
||||||
return {heroes};
|
return {heroes};
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
{
|
{
|
||||||
"data": [
|
"data": [
|
||||||
{ "id": "1", "name": "Windstorm" },
|
{ "id": 1, "name": "Windstorm" },
|
||||||
{ "id": "2", "name": "Bombasto" },
|
{ "id": 2, "name": "Bombasto" },
|
||||||
{ "id": "3", "name": "Magneta" },
|
{ "id": 3, "name": "Magneta" },
|
||||||
{ "id": "4", "name": "Tornado" }
|
{ "id": 4, "name": "Tornado" }
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
// import 'rxjs/Rx'; // adds ALL RxJS statics & operators to Observable
|
// import 'rxjs/Rx'; // adds ALL RxJS statics & operators to Observable
|
||||||
|
|
||||||
// See node_module/rxjs/Rxjs.js
|
// See node_module/rxjs/Rxjs.js
|
||||||
// Import just the rxjs statics and operators we need for THIS app.
|
// Import just the rxjs statics and operators needed for THIS app.
|
||||||
|
|
||||||
// Statics
|
// Statics
|
||||||
import 'rxjs/add/observable/throw';
|
import 'rxjs/add/observable/throw';
|
||||||
|
@ -2,13 +2,10 @@
|
|||||||
<h1>Tour of Heroes ({{mode}})</h1>
|
<h1>Tour of Heroes ({{mode}})</h1>
|
||||||
<h3>Heroes:</h3>
|
<h3>Heroes:</h3>
|
||||||
<ul>
|
<ul>
|
||||||
<li *ngFor="let hero of heroes">
|
<li *ngFor="let hero of heroes">{{hero.name}}</li>
|
||||||
{{hero.name}}
|
|
||||||
</li>
|
|
||||||
</ul>
|
</ul>
|
||||||
New hero name:
|
|
||||||
<input #newHeroName />
|
<label>New hero name: <input #newHeroName /></label>
|
||||||
<button (click)="addHero(newHeroName.value); newHeroName.value=''">
|
<button (click)="addHero(newHeroName.value); newHeroName.value=''">Add Hero</button>
|
||||||
Add Hero
|
|
||||||
</button>
|
<p class="error" *ngIf="errorMessage">{{errorMessage}}</p>
|
||||||
<div class="error" *ngIf="errorMessage">{{errorMessage}}</div>
|
|
||||||
|
@ -8,7 +8,8 @@ import { HeroService } from './hero.service.promise';
|
|||||||
selector: 'hero-list-promise',
|
selector: 'hero-list-promise',
|
||||||
moduleId: module.id,
|
moduleId: module.id,
|
||||||
templateUrl: 'hero-list.component.html',
|
templateUrl: 'hero-list.component.html',
|
||||||
providers: [ HeroService ]
|
providers: [ HeroService ],
|
||||||
|
styles: ['.error {color:red;}']
|
||||||
})
|
})
|
||||||
// #docregion component
|
// #docregion component
|
||||||
export class HeroListPromiseComponent implements OnInit {
|
export class HeroListPromiseComponent implements OnInit {
|
||||||
|
@ -8,7 +8,8 @@ import { HeroService } from './hero.service';
|
|||||||
moduleId: module.id,
|
moduleId: module.id,
|
||||||
selector: 'hero-list',
|
selector: 'hero-list',
|
||||||
templateUrl: 'hero-list.component.html',
|
templateUrl: 'hero-list.component.html',
|
||||||
providers: [ HeroService ]
|
providers: [ HeroService ],
|
||||||
|
styles: ['.error {color:red;}']
|
||||||
})
|
})
|
||||||
// #docregion component
|
// #docregion component
|
||||||
export class HeroListComponent implements OnInit {
|
export class HeroListComponent implements OnInit {
|
||||||
|
@ -9,7 +9,7 @@ import { Hero } from './hero';
|
|||||||
@Injectable()
|
@Injectable()
|
||||||
export class HeroService {
|
export class HeroService {
|
||||||
// URL to web api
|
// URL to web api
|
||||||
private heroesUrl = 'app/heroes.json';
|
private heroesUrl = 'app/heroes';
|
||||||
|
|
||||||
constructor (private http: Http) {}
|
constructor (private http: Http) {}
|
||||||
|
|
||||||
@ -22,11 +22,10 @@ export class HeroService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
addHero (name: string): Promise<Hero> {
|
addHero (name: string): Promise<Hero> {
|
||||||
let body = JSON.stringify({ name });
|
|
||||||
let headers = new Headers({ 'Content-Type': 'application/json' });
|
let headers = new Headers({ 'Content-Type': 'application/json' });
|
||||||
let options = new RequestOptions({ headers: headers });
|
let options = new RequestOptions({ headers: headers });
|
||||||
|
|
||||||
return this.http.post(this.heroesUrl, body, options)
|
return this.http.post(this.heroesUrl, { name }, options)
|
||||||
.toPromise()
|
.toPromise()
|
||||||
.then(this.extractData)
|
.then(this.extractData)
|
||||||
.catch(this.handleError);
|
.catch(this.handleError);
|
||||||
@ -37,12 +36,17 @@ export class HeroService {
|
|||||||
return body.data || { };
|
return body.data || { };
|
||||||
}
|
}
|
||||||
|
|
||||||
private handleError (error: any) {
|
private handleError (error: Response | any) {
|
||||||
// In a real world app, we might use a remote logging infrastructure
|
// In a real world app, we might use a remote logging infrastructure
|
||||||
// We'd also dig deeper into the error to get a better message
|
let errMsg: string;
|
||||||
let errMsg = (error.message) ? error.message :
|
if (error instanceof Response) {
|
||||||
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
|
const body = error.json() || '';
|
||||||
console.error(errMsg); // log to console instead
|
const err = body.error || JSON.stringify(body);
|
||||||
|
errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
|
||||||
|
} else {
|
||||||
|
errMsg = error.message ? error.message : error.toString();
|
||||||
|
}
|
||||||
|
console.error(errMsg);
|
||||||
return Promise.reject(errMsg);
|
return Promise.reject(errMsg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,11 +34,10 @@ export class HeroService {
|
|||||||
// #docregion addhero, addhero-sig
|
// #docregion addhero, addhero-sig
|
||||||
addHero (name: string): Observable<Hero> {
|
addHero (name: string): Observable<Hero> {
|
||||||
// #enddocregion addhero-sig
|
// #enddocregion addhero-sig
|
||||||
let body = JSON.stringify({ name });
|
|
||||||
let headers = new Headers({ 'Content-Type': 'application/json' });
|
let headers = new Headers({ 'Content-Type': 'application/json' });
|
||||||
let options = new RequestOptions({ headers: headers });
|
let options = new RequestOptions({ headers: headers });
|
||||||
|
|
||||||
return this.http.post(this.heroesUrl, body, options)
|
return this.http.post(this.heroesUrl, { name }, options)
|
||||||
.map(this.extractData)
|
.map(this.extractData)
|
||||||
.catch(this.handleError);
|
.catch(this.handleError);
|
||||||
}
|
}
|
||||||
@ -50,14 +49,19 @@ export class HeroService {
|
|||||||
return body.data || { };
|
return body.data || { };
|
||||||
}
|
}
|
||||||
// #enddocregion extract-data
|
// #enddocregion extract-data
|
||||||
|
|
||||||
// #docregion error-handling
|
// #docregion error-handling
|
||||||
private handleError (error: any) {
|
|
||||||
|
private handleError (error: Response | any) {
|
||||||
// In a real world app, we might use a remote logging infrastructure
|
// In a real world app, we might use a remote logging infrastructure
|
||||||
// We'd also dig deeper into the error to get a better message
|
let errMsg: string;
|
||||||
let errMsg = (error.message) ? error.message :
|
if (error instanceof Response) {
|
||||||
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
|
const body = error.json() || '';
|
||||||
console.error(errMsg); // log to console instead
|
const err = body.error || JSON.stringify(body);
|
||||||
|
errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
|
||||||
|
} else {
|
||||||
|
errMsg = error.message ? error.message : error.toString();
|
||||||
|
}
|
||||||
|
console.error(errMsg);
|
||||||
return Observable.throw(errMsg);
|
return Observable.throw(errMsg);
|
||||||
}
|
}
|
||||||
// #enddocregion error-handling, methods
|
// #enddocregion error-handling, methods
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
/* tslint:disable: member-ordering forin */
|
||||||
|
// #docplaster
|
||||||
// #docregion
|
// #docregion
|
||||||
import { Component } from '@angular/core';
|
import { Component } from '@angular/core';
|
||||||
import { Observable } from 'rxjs/Observable';
|
import { Observable } from 'rxjs/Observable';
|
||||||
@ -8,33 +10,27 @@ import { Subject } from 'rxjs/Subject';
|
|||||||
import { WikipediaService } from './wikipedia.service';
|
import { WikipediaService } from './wikipedia.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
|
moduleId: module.id,
|
||||||
selector: 'my-wiki-smart',
|
selector: 'my-wiki-smart',
|
||||||
template: `
|
templateUrl: 'wiki.component.html',
|
||||||
<h1>Smarter Wikipedia Demo</h1>
|
providers: [ WikipediaService ]
|
||||||
<p><i>Fetches when typing stops</i></p>
|
|
||||||
|
|
||||||
<input #term (keyup)="search(term.value)"/>
|
|
||||||
|
|
||||||
<ul>
|
|
||||||
<li *ngFor="let item of items | async">{{item}}</li>
|
|
||||||
</ul>
|
|
||||||
`,
|
|
||||||
providers: [WikipediaService]
|
|
||||||
})
|
})
|
||||||
export class WikiSmartComponent {
|
export class WikiSmartComponent {
|
||||||
|
title = 'Smarter Wikipedia Demo';
|
||||||
constructor (private wikipediaService: WikipediaService) { }
|
fetches = 'Fetches when typing stops';
|
||||||
|
items: Observable<string[]>;
|
||||||
|
|
||||||
// #docregion subject
|
// #docregion subject
|
||||||
private searchTermStream = new Subject<string>();
|
private searchTermStream = new Subject<string>();
|
||||||
|
|
||||||
search(term: string) { this.searchTermStream.next(term); }
|
search(term: string) { this.searchTermStream.next(term); }
|
||||||
// #enddocregion subject
|
// #enddocregion subject
|
||||||
|
|
||||||
// #docregion observable-operators
|
constructor (private wikipediaService: WikipediaService) {
|
||||||
items: Observable<string[]> = this.searchTermStream
|
// #docregion observable-operators
|
||||||
.debounceTime(300)
|
this.items = this.searchTermStream
|
||||||
.distinctUntilChanged()
|
.debounceTime(300)
|
||||||
.switchMap((term: string) => this.wikipediaService.search(term));
|
.distinctUntilChanged()
|
||||||
// #enddocregion observable-operators
|
.switchMap((term: string) => this.wikipediaService.search(term));
|
||||||
|
// #enddocregion observable-operators
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,11 @@
|
|||||||
|
<!-- #docregion -->
|
||||||
|
<h1>{{title}}</h1>
|
||||||
|
<p><i>{{fetches}}</i></p>
|
||||||
|
|
||||||
|
<!-- #docregion keyup -->
|
||||||
|
<input #term (keyup)="search(term.value)"/>
|
||||||
|
<!-- #enddocregion keyup -->
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li *ngFor="let item of items | async">{{item}}</li>
|
||||||
|
</ul>
|
@ -5,25 +5,19 @@ import { Observable } from 'rxjs/Observable';
|
|||||||
import { WikipediaService } from './wikipedia.service';
|
import { WikipediaService } from './wikipedia.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
|
moduleId: module.id,
|
||||||
selector: 'my-wiki',
|
selector: 'my-wiki',
|
||||||
template: `
|
templateUrl: 'wiki.component.html',
|
||||||
<h1>Wikipedia Demo</h1>
|
providers: [ WikipediaService ]
|
||||||
<p><i>Fetches after each keystroke</i></p>
|
|
||||||
|
|
||||||
<input #term (keyup)="search(term.value)"/>
|
|
||||||
|
|
||||||
<ul>
|
|
||||||
<li *ngFor="let item of items | async">{{item}}</li>
|
|
||||||
</ul>
|
|
||||||
`,
|
|
||||||
providers: [WikipediaService]
|
|
||||||
})
|
})
|
||||||
export class WikiComponent {
|
export class WikiComponent {
|
||||||
|
title = 'Wikipedia Demo';
|
||||||
|
fetches = 'Fetches after each keystroke';
|
||||||
items: Observable<string[]>;
|
items: Observable<string[]>;
|
||||||
|
|
||||||
constructor (private wikipediaService: WikipediaService) {}
|
|
||||||
|
|
||||||
search (term: string) {
|
search (term: string) {
|
||||||
this.items = this.wikipediaService.search(term);
|
this.items = this.wikipediaService.search(term);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
constructor (private wikipediaService: WikipediaService) { }
|
||||||
}
|
}
|
||||||
|
@ -6,8 +6,6 @@
|
|||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
<link rel="stylesheet" href="styles.css">
|
<link rel="stylesheet" href="styles.css">
|
||||||
<link rel="stylesheet" href="sample.css">
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Polyfill(s) for older browsers -->
|
<!-- Polyfill(s) for older browsers -->
|
||||||
<script src="node_modules/core-js/client/shim.min.js"></script>
|
<script src="node_modules/core-js/client/shim.min.js"></script>
|
||||||
|
@ -1 +0,0 @@
|
|||||||
.error {color:red;}
|
|
@ -108,7 +108,7 @@
|
|||||||
|
|
||||||
"server-communication": {
|
"server-communication": {
|
||||||
"title": "HTTP Client",
|
"title": "HTTP Client",
|
||||||
"intro": "Talk to a remote server with an HTTP Client."
|
"intro": "Use an HTTP Client to talk to a remote server."
|
||||||
},
|
},
|
||||||
|
|
||||||
"lifecycle-hooks": {
|
"lifecycle-hooks": {
|
||||||
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user