Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit 8cf2b1f

Browse files
committed
docs(toh6-http): ward's tweaks
1 parent 86668a0 commit 8cf2b1f

File tree

5 files changed

+86
-70
lines changed

5 files changed

+86
-70
lines changed

public/docs/_examples/toh-6/ts/app/hero-detail.component.ts

+15-11
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import { HeroService } from './hero.service';
1212
})
1313
export class HeroDetailComponent implements OnInit {
1414
@Input() hero: Hero;
15-
@Output() updateHeroes = new EventEmitter();
16-
error:any;
15+
@Output() close = new EventEmitter();
16+
error: any;
17+
navigated = false; // true if navigated here
1718

1819
constructor(
1920
private _heroService: HeroService,
@@ -22,28 +23,31 @@ export class HeroDetailComponent implements OnInit {
2223

2324
// #docregion ngOnInit
2425
ngOnInit() {
25-
if(this._routeParams.get('id') !== null){
26+
if (this._routeParams.get('id') !== null) {
2627
let id = +this._routeParams.get('id');
28+
this.navigated = true;
2729
this._heroService.getHero(id)
2830
.then(hero => this.hero = hero);
29-
}
30-
else{
31+
} else {
32+
this.navigated = false;
3133
this.hero = new Hero();
3234
}
3335
}
3436
// #enddocregion ngOnInit
3537
// #docregion save
36-
save(){
38+
save() {
3739
this._heroService
3840
.save(this.hero)
39-
.then(response => {
40-
this.updateHeroes.emit(response);
41+
.then(hero => {
42+
this.hero = hero; // saved hero, w/ id if new
43+
this.goBack(hero);
4144
})
42-
.catch(error => this.error = error);//TODO: Display error message
45+
.catch(error => this.error = error); // TODO: Display error message
4346
}
4447
// #enddocregion save
45-
goBack() {
46-
window.history.back();
48+
goBack(savedHero: Hero = null) {
49+
this.close.emit(savedHero);
50+
if (this.navigated) { window.history.back(); }
4751
}
4852
}
4953

public/docs/_examples/toh-6/ts/app/hero.service.ts

+48-45
Original file line numberDiff line numberDiff line change
@@ -6,80 +6,83 @@ import { Http, Response, Headers } from 'angular2/http';
66

77
@Injectable()
88
export class HeroService {
9-
9+
1010
private _heroesUrl = 'app/heroes'; // URL to web api
11-
12-
constructor(private _http:Http){
13-
}
14-
15-
//#docregion get-heroes
16-
getHeroes() {
11+
12+
constructor(private _http: Http) { }
13+
14+
// #docregion get-heroes
15+
getHeroes(): Promise<Hero[]> {
1716
return this._http
1817
.get(this._heroesUrl).toPromise()
19-
.then((res:Response) => res.json().data)
18+
.then(response => response.json().data)
2019
.catch(this._handleError);
2120
}
22-
//#enddocregion get-heroes
23-
21+
// #enddocregion get-heroes
22+
2423
getHero(id: number) {
2524
return this.getHeroes()
26-
.then(heroes => heroes.filter((hero:Hero) => hero.id === id)[0]);
25+
.then(heroes => heroes.filter(hero => hero.id === id)[0]);
2726
}
28-
29-
//#docregion save
30-
save(hero:Hero){
31-
if(hero.id){
27+
28+
// #docregion save
29+
save(hero: Hero): Promise<Hero> {
30+
if (hero.id) {
3231
return this._put(hero);
3332
}
3433
return this._post(hero);
3534
}
36-
//#enddocregion save
37-
38-
//#docregion delete-hero
39-
delete(hero:Hero){
35+
// #enddocregion save
36+
37+
// #docregion delete-hero
38+
delete(hero: Hero) {
4039
let headers = new Headers();
4140
headers.append('Content-Type', 'application/json');
42-
41+
4342
let url = `${this._heroesUrl}/${hero.id}`;
44-
43+
4544
return this._http
46-
.delete(url,headers)
45+
.delete(url, headers)
4746
.toPromise()
4847
.catch(this._handleError);
4948
}
50-
//#enddocregion delete-hero
51-
52-
//#docregion post-hero
53-
private _post(hero:Hero){
54-
let headers = new Headers();
55-
headers.append('Content-Type', 'application/json');
56-
49+
// #enddocregion delete-hero
50+
51+
// #docregion post-hero
52+
// Add new Hero
53+
private _post(hero: Hero): Promise<Hero> {
54+
let headers = new Headers({
55+
'Content-Type': 'application/json'});
56+
5757
return this._http
58-
.post(this._heroesUrl, JSON.stringify(hero), {headers:headers})
58+
.post(this._heroesUrl, JSON.stringify(hero), {headers: headers})
5959
.toPromise()
60+
.then(res => res.json().data)
6061
.catch(this._handleError);
6162
}
62-
//#enddocregion post-hero
63-
64-
//#docregion put-hero
65-
private _put(hero:Hero){
63+
// #enddocregion post-hero
64+
65+
// #docregion put-hero
66+
// Update existing Hero
67+
private _put(hero: Hero) {
6668
let headers = new Headers();
6769
headers.append('Content-Type', 'application/json');
68-
70+
6971
let url = `${this._heroesUrl}/${hero.id}`;
70-
72+
7173
return this._http
72-
.put(url, JSON.stringify(hero), {headers:headers})
74+
.put(url, JSON.stringify(hero), {headers: headers})
7375
.toPromise()
76+
.then(() => hero)
7477
.catch(this._handleError);
7578
}
76-
//#enddocregion put-hero
77-
78-
//#docregion error-handler
79-
private _handleError(error:any){
80-
console.log('An error occurred:' + error);
81-
return Promise.reject(error);
79+
// #enddocregion put-hero
80+
81+
// #docregion error-handler
82+
private _handleError(error: any) {
83+
console.error('An error occurred', error);
84+
return Promise.reject(error.message || error);
8285
}
83-
//#enddocregion error-handler
86+
// #enddocregion error-handler
8487
}
85-
// #enddocregion
88+
// #enddocregion

public/docs/_examples/toh-6/ts/app/heroes.component.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ <h2>My Heroes</h2>
1111

1212
<button (click)="addHero()">Add New Hero</button>
1313
<div *ngIf="addingHero">
14-
<my-hero-detail (updateHeroes)="getHeroes()"></my-hero-detail>
14+
<my-hero-detail (close)="close($event)"></my-hero-detail>
1515
</div>
1616
<div *ngIf="selectedHero">
1717
<h2>

public/docs/_examples/toh-6/ts/app/heroes.component.ts

+15-10
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class HeroesComponent implements OnInit {
1616
heroes: Hero[];
1717
selectedHero: Hero;
1818
addingHero = false;
19-
error:any;
19+
error: any;
2020

2121
constructor(
2222
private _router: Router,
@@ -26,32 +26,37 @@ export class HeroesComponent implements OnInit {
2626
this._heroService
2727
.getHeroes()
2828
.then(heroes => this.heroes = heroes)
29-
.catch(error => this.error = error);//TODO: Display error message
29+
.catch(error => this.error = error); // TODO: Display error message
3030
}
31-
32-
addHero(){
31+
32+
addHero() {
3333
this.addingHero = true;
3434
this.selectedHero = null;
3535
}
36-
36+
37+
close(savedHero: Hero) {
38+
this.addingHero = false;
39+
if (savedHero) { this.getHeroes(); }
40+
}
41+
3742
// #docregion delete
38-
delete(hero:Hero, event:any){
39-
event.stopPropagation();
43+
delete(hero: Hero, event: any) {
44+
event.stopPropagation();
4045
this._heroService
4146
.delete(hero)
4247
.then(res => {
4348
this.heroes = this.heroes.filter(h => h.id !== hero.id);
4449
})
45-
.catch(error => this.error = error);//TODO: Display error message
50+
.catch(error => this.error = error); // TODO: Display error message
4651
}
4752
// #enddocregion delete
4853

4954
ngOnInit() {
5055
this.getHeroes();
5156
}
5257

53-
onSelect(hero: Hero) {
54-
this.selectedHero = hero;
58+
onSelect(hero: Hero) {
59+
this.selectedHero = hero;
5560
this.addingHero = false;
5661
}
5762

public/docs/ts/latest/tutorial/toh-pt6.jade

+7-3
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,18 @@ code-example(format="." language="bash").
7272

7373
In our previous implementation we created a promise ourselves and resolved it immediately. When using `Http` we no longer have to create the promise manually. Instead `Http` will create it for us, but we have to call `toPromise()` to get it.
7474

75-
In the `then` we convert the response to a json object by calling `res.json()`.
75+
In the `then` we convert the response to a json object by calling `response.json()`.
7676

77-
Our api follows the convention of returning the result wrapped in a `data` property, but we don't want to expose this to the caller, so we flatten the response by returning `res.data`.
77+
Our api follows the convention of returning the result wrapped in a `data` property, but we don't want to expose this to the caller, so we flatten the response by returning `response.data`.
7878

7979
:marked
8080
### Error Handling
8181

82-
At the end we are calling `catch` and passing in an error handler. This is an important step since it allows us to catch any error returned by the api. In the service we log the error and return the rejected promise. It's important that the service returns the rejected promise to the component, so that we can display an error message to the user.
82+
At the end we are calling `catch` and passing in an error handler.
83+
This is an important step since it allows us to catch any error returned by the api.
84+
In the service we log the error and return the rejected promise.
85+
It's important that the service returns some form &mdash; perhaps a user friendly form &mdash;
86+
of the rejected promise to the component, so that we can display a proper error message to the user.
8387
+makeExample('toh-6/ts/app/hero.service.ts', 'error-handler', 'app/hero.service.ts (Error handler)')(format=".")
8488

8589
:marked

0 commit comments

Comments
 (0)