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.
We've also decided to return a user friendly form of the error to
162
162
to the caller in a rejected promise so that the caller can display a proper error message to the user.
163
-
163
+
164
164
### Promises are Promises
165
-
Although we made significant *internal* changes to `getHeroes()`, the public signature did not change.
165
+
Although we made significant *internal* changes to `getHeroes()`, the public signature did not change.
166
166
We still return a promise. We won't have to update any of the components that call `getHeroes()`.
167
167
168
-
.l-main-section
168
+
.l-main-section
169
169
:marked
170
170
## Add, Edit, Delete
171
-
172
-
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.
173
-
174
-
We'll complete `HeroService` by creating `post`, `put` and `delete` http calls to meet our new requirements.
171
+
172
+
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.
173
+
174
+
We'll complete `HeroService` by creating `post`, `put` and `delete` http calls to meet our new requirements.
175
175
176
176
:marked
177
177
### Post
178
-
178
+
179
179
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:
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.
185
-
184
+
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.
185
+
186
186
### Put
187
-
187
+
188
188
`put` is used to edit a specific hero, but the structure is very similar to a `post` request. The only difference is that we have to change the url slightly by appending the id of the hero we want to edit.
We add a `catch` to handle our errors for all three cases.
200
-
200
+
201
201
:marked
202
202
### Save
203
203
204
204
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.
Loading heroes using `Http` required no changes outside of `HeroService`, but we added a few new features as well.
216
+
217
+
Loading heroes using `Http` required no changes outside of `HeroService`, but we added a few new features as well.
218
218
In the following section we will update our components to use our new methods to add, edit and delete heroes.
219
-
219
+
220
220
### Add/Edit in the *HeroDetailComponent*
221
-
We already have `HeroDetailComponent` for viewing details about a specific hero. Add and Edit are natural extensions of the detail view, so we are able to reuse `HeroDetailComponent` with a few tweaks. The original component was created to render existing data, but to add new data we have to initialize the `hero` property to an empty `Hero` object.
222
-
221
+
222
+
We already have `HeroDetailComponent` for viewing details about a specific hero.
223
+
Add and Edit are natural extensions of the detail view, so we are able to reuse `HeroDetailComponent` with a few tweaks.
224
+
The original component was created to render existing data, but to add new data we have to initialize the `hero` property to an empty `Hero` object.
In order to differentiate between add and edit we are adding a check to see if an id is passed in the url. If the id is absent we bind `HeroDetailComponent` to an empty `Hero` object. In either case, any edits made through the UI will be bound back to the same `hero` property.
227
230
228
231
The next step is to add a save method to `HeroDetailComponent` and call the corresponding save method in `HeroesService`.
Here we call `emit` to notify 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.
241
244
242
245
.l-sub-section
243
246
:marked
244
-
The `emit` "handshake" between `HeroDetailComponent` and `HeroesComponent` is an example of component to component communication. This is a topic for another day, but we have detailed information in our <a href="/docs/ts/latest/cookbook/component-communication.html#!#child-to-parent">Component Interaction Cookbook</a>
247
+
The `emit` "handshake" between `HeroDetailComponent` and `HeroesComponent` is an example of component to component communication. This is a topic for another day, but we have detailed information in our <a href="/docs/ts/latest/cookbook/component-communication.html#!#child-to-parent">Component Interaction Cookbook</a>
245
248
246
249
:marked
247
250
Here is `HeroDetailComponent` with its new save button.
248
-
251
+
249
252
figure.image-display
250
253
img(src='/resources/images/devguide/toh/hero-details-save-button.png'alt="Hero Details With Save Button")
251
254
252
255
:marked
253
256
### Add/Delete in the *HeroesComponent*
254
-
257
+
255
258
The user can *add* a new hero by clicking a button and entering a name.
256
259
257
260
When the user clicks the *Add New Hero* button, we display the `HeroDetailComponent`.
258
261
We aren't navigating to the component so it won't receive a hero `id`;
259
262
As we noted above, that is the component's cue to create and present an empty hero.
260
-
263
+
261
264
Add the following HTML to the `heroes.component.html`, just below the hero list (the `*ngFor`).
0 commit comments