You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 4, 2017. It is now read-only.
Copy file name to clipboardExpand all lines: public/docs/ts/latest/tutorial/toh-pt6.jade
+48-32Lines changed: 48 additions & 32 deletions
Original file line number
Diff line number
Diff line change
@@ -3,11 +3,11 @@ include ../_util-fns
3
3
:marked
4
4
# Http
5
5
6
-
Our application has become a huge success and our stakeholders have already expanded the vision to include integration with a hero api.
6
+
Our application has become a huge success and our stakeholders have expanded the vision to include integration with a hero api.
7
7
8
-
The current solution limits us to a fixed set of heroes, but integration with the api will allow us to load heroes dynamically from a database. We will also be able to add, edit and delete heroes.
8
+
The current solution limits us to a fixed set of heroes, but integration with a web server api will make our application much more flexible. We will also be able to add, edit and delete heroes.
9
9
10
-
In this chapter we will show how to integrate the http based api with our existing application.
10
+
In this chapter we will connect our Angular 2 services to make http calls to our new api.
11
11
:marked
12
12
[Run the live example](/resources/live-examples/toh-6/ts/plnkr.html).
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.
74
74
75
-
In the first `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 `res.json()`.
76
76
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 in the second `then` 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 `res.data`.
78
78
79
79
:marked
80
80
### Error Handling
81
81
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.
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.
We want to point out that a promise is not the only way to process http calls. We will encounter other approaches such as Observables in subsequent chapters, but for the purposes of this article we are sticking to promises.
91
+
We realize there are alternatives to promises for processing http calls. We will encounter other approaches such as Observables in subsequent chapters, but for the purposes of this article we are sticking to promises.
92
92
93
93
.l-main-section
94
94
:marked
95
95
## Add, Edit, Delete
96
96
97
97
Our stakeholders are incredibly pleased with the added flexibility from the api integration, but it doesn't stop there. Next we want to add the capability to add, edit and delete heroes.
98
98
99
-
In this section we will show how to implement http `post`, `put` and `delete` calls in `HeroService` to meet our new requirements.
99
+
We'll complete `HeroService` by creating `post`, `put` and `delete` http calls to meet our new requirements.
100
100
101
101
:marked
102
102
### Post
103
103
104
-
We are using `post` to add new `Hero` objects to our database. Post requests require a little bit more setup than Get requests, but the format is as follows:
104
+
We are using `post` to add new heroes. Post requests require a little bit more setup than Get requests, but the format is as follows:
First we create a header and set the content type to `application/json`. Before posting we have to call `JSON.stringify` to convert the hero object to a string.
109
+
Now we create a header and set the content type to `application/json`. We'll call `JSON.stringify` before we post to convert the hero object to a string.
In all three cases we add a `catch` for error handling.
124
+
We add a `catch` to handle our errors for all three cases.
125
125
126
126
:marked
127
127
### Save
128
128
129
-
I am combining the call to the private `_post` and `_put` methods in a single `save` method. This simplifies the public api and makes the integration with `HeroDetailComponent` easier. `HeroService` determines which method to call based on the state of the `hero` object. If the hero already has an id we know it's an edit. Otherwise we know it's an add.
129
+
We combine the call to the private `_post` and `_put` methods in a single `save` method. This simplifies the public api and makes the integration with `HeroDetailComponent` easier. `HeroService` determines which method to call based on the state of the `hero` object. If the hero already has an id we know it's an edit. Otherwise we know it's an add.
The same save method is used for both add end edit since `HeroService` will know when to call `post` vs `put` based on the state of the `Hero` object.
157
+
The same save method is used for both add and edit since `HeroService` will know when to call `post` vs `put` based on the state of the `Hero` object.
158
158
159
-
We mentioned earlier that `save()` returns a promise, so when the promise resolves, we call `emit` to notify `HeroesComponent` that we just added or modified a hero. `HeroesComponent` is listening for this notification and will automatically refresh the list of heroes to include our recent updates.
159
+
Earlier we used the `save()` method to return a promise, so when the promise resolves, we call `emit` to notify `HeroesComponent` that we just added or modified a hero. `HeroesComponent` is listening for this notification and will automatically refresh the list of heroes to include our recent updates.
160
160
161
161
.l-sub-section
162
162
:marked
@@ -185,7 +185,7 @@ figure.image-display
185
185
:marked
186
186
## Integration with Http
187
187
188
-
`Http` is not part of Angular core, but exists as a separate add-on module called `angular2/http`. It ships as a separate bundle, so we have to include the bundle in `index.html`.
188
+
`Http` is not part of the Angular core module, but exists as a separate add-on module called `angular2/http`. It ships as a separate bundle, so we have to include the bundle in `index.html`.
Our hero api is not deployed to production yet, but for the purposes of this chapter we have decided to integrate with an in-memory web api simulator.
199
+
Our hero api is not deployed to production yet, but for the purposes of this chapter we have decided to integrate with an in-memory web api.
200
200
201
-
If you look at `AppComponent` you will notice that we have also imported `InMemoryBackendService`, `HeroData` and `SEED_DATA`. These imports are only there to configure the api simulator and will not be needed when integrating with a real api.
201
+
Looking at the `AppComponent` we notice that we have also imported `InMemoryBackendService`, `InMemoryDataService` and `SEED_DATA`. These imports are only there to configure the in-memory web api and will not be needed when integrating with a real api.
202
202
203
-
We have included some more information about configuring the simulator <a href='/docs/ts/latest/guide/server-communication.html#!#in-mem-web-api'>here</a>.
203
+
We have included some more information about configuring the in-memory web api <a href='/docs/ts/latest/guide/server-communication.html#!#in-mem-web-api'>here</a>.
204
204
205
-
Since we are integrating with the simulator we no longer need `mock-heroes.ts`, so it's safe to delete it.
205
+
Since we are integrating with the in-memory web api we no longer need `mock-heroes.ts`, so it's safe to delete it.
206
206
207
207
:marked
208
208
### Review the App Structure
@@ -227,7 +227,7 @@ figure.image-display
227
227
.file heroes.component.html
228
228
.file heroes.component.ts
229
229
.file main.ts
230
-
.file hero-data.ts
230
+
.file hero-data.service.ts
231
231
.file node_modules ...
232
232
.file typings ...
233
233
.file index.html
@@ -236,17 +236,33 @@ figure.image-display
236
236
.file sample.css
237
237
.file tsconfig.json
238
238
.file typings.json
239
-
240
-
:marked
241
-
242
-
.l-main-section
239
+
240
+
.l-main-section
243
241
:marked
244
-
## Recap
242
+
## Home Stretch
243
+
244
+
We are at the end of our journey for now, but we have accomplished a lot.
245
+
- We added the necessary dependencies to use Http in our application.
246
+
- We refactored HeroService to load heroes from an api.
247
+
- We extended HeroService to support post, put and delete calls.
248
+
- We updated our components to allow adding, editing and deleting of heroes.
249
+
- We configured an in-memory web api.
250
+
251
+
Below is a summary of the files we changed.
252
+
253
+
+makeTabs(
254
+
`toh-6/ts/app/app.component.ts,
255
+
toh-6/ts/app/heroes.component.ts,
256
+
toh-6/ts/app/heroes.component.html,
257
+
toh-6/ts/app/hero-detail.component.ts,
258
+
toh-6/ts/app/hero-detail.component.html,
259
+
toh-6/ts/app/hero.service.ts`,
260
+
null,
261
+
`app.comp...ts,
262
+
heroes.comp...ts,
263
+
heroes.comp...html,
264
+
hero-detail.comp...ts,
265
+
hero-detail.comp...html,
266
+
hero.service.ts`
267
+
)
245
268
246
-
### The Road Behind
247
-
We travelled a great distance in this chapter.
248
-
- We add the necessary dependencies to use Http in our application.
249
-
- We refactored HeroService to load heroes from a database.
250
-
- We extended HeroService to support post, put and delete calls.
251
-
- We updated our components to allow adding, editing and deleting of heroes.
0 commit comments