@@ -60,6 +60,7 @@ figure.image-display
60
60
:marked
61
61
Here is the `TohComponent` shell:
62
62
+ makeExample('server-communication/ts/app/toh/toh.component.ts' , '' , 'app/toh/toh.component.ts' )
63
+
63
64
block http-providers
64
65
:marked
65
66
As usual, we import the symbols we need. The newcomer is `HTTP_PROVIDERS`,
@@ -191,18 +192,26 @@ block rxjs
191
192
We should include only those features that we actually need.
192
193
193
194
Accordingly, Angular exposes a stripped down version of `Observable` in the `rxjs/Observable` module,
194
- a version that lacks almost all operators including the ones we'd like to use here
195
+ a version that lacks most of the operators including some we'd like to use here
195
196
such as the `map` method we called above in `getHeroes`.
196
197
197
198
It's up to us to add the operators we need.
198
- We could add each operator, one-by-one, until we had a custom *Observable* implementation tuned
199
- precisely to our requirements.
200
-
201
- That would be a distraction today. We're learning HTTP, not counting bytes.
202
- So we'll make it easy on ourselves and enrich *Observable* with the full set of operators.
203
- It only takes one `import` statement.
204
- It's best to add that statement early when we're bootstrapping the application.
205
- :
199
+
200
+ We could add _every_ RxJS operators with a single import statement.
201
+ While that is the easiest thing to do, we'd pay a penalty in extended launch time and application size
202
+ because the full library is so big. We only use a few operators in our app.
203
+
204
+ Instead, we'll import each operator, one-by-one, until we have a custom *Observable* implementation tuned
205
+ precisely to our requirements. We'll put the `import` statements in one `app/add-rxjs-operators.ts` file.
206
+ + makeExample('server-communication/ts/app/add-rxjs-operators.ts' , null , 'app/add-rxjs-operators.ts' )( format ="." )
207
+ :marked
208
+ If we forget an operator, the compiler will warn that it's missing and we'll update this file.
209
+ .l-sub-section
210
+ :marked
211
+ We don't need _all_ of these particular operators in the `HeroService` — just `map` and `catch`.
212
+ We'll need the others later, in a *Wiki* example [below](#more-observables).
213
+ :marked
214
+ Finally, we import `add-rxjs-operator`_itself_ in our `main.ts`:
206
215
+ makeExample('server-communication/ts/app/main.ts' , 'import-rxjs' , 'app/main.ts (import rxjs)' )( format ="." )
207
216
208
217
a#extract-data
@@ -212,31 +221,8 @@ a#extract-data
212
221
+ makeExample('server-communication/ts/app/toh/hero.service.ts' , 'extract-data' , 'app/toh/hero.service.ts (excerpt)' )( format ="." )
213
222
:marked
214
223
The `response` object does not hold our data in a form we can use directly.
215
- To make it useful in our application we must
216
- * check the response status,
217
- * parse the response data into a JSON object
224
+ To make it useful in our application we must parse the response data into a JSON object
218
225
219
- + ifDocsFor('ts' )
220
- .alert.is-important
221
- :marked
222
- *Beta alert*: error status interception and parsing may be absorbed within `http` when Angular is released.
223
-
224
- :marked
225
- #### Non-success status codes
226
- A status code outside the 200-299 range denotes an error from the _application point of view_
227
- but it is not an error from the _HTTP point of view_.
228
- For example, a `404 - Not Found` is a response like any other.
229
- The request went out; a response came back; here it is, thank you very much.
230
- We'd have an exception only if `#{_priv}http` failed to operate (e.g., it errored internally).
231
-
232
- block non-success-status-codes
233
- :marked
234
- Because a status code outside the 200-299 range _is an error_ from the application point of view,
235
- we intercept it and throw, moving the observable chain to the error path.
236
-
237
- The `catch` operator that is next in the `getHeroes` observable chain will handle our thrown error.
238
-
239
- :marked
240
226
#### Parse to JSON
241
227
block parse-json
242
228
:marked
@@ -597,7 +583,7 @@ block wikipedia-jsonp+
597
583
+ makeExample('server-communication/ts/app/wiki/wiki-smart.component.ts' , 'observable-operators' )( format ='.' )
598
584
:marked
599
585
We wait for the user to stop typing for at least 300 milliseconds
600
- ([debounce ](https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/debounce.md)).
586
+ ([debounceTime ](https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/debounce.md)).
601
587
Only changed search values make it through to the service
602
588
([distinctUntilChanged](https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/distinctuntilchanged.md)).
603
589
@@ -611,6 +597,10 @@ block wikipedia-jsonp+
611
597
and delivers to subscribers only the most recent search results.
612
598
613
599
The displayed list of search results stays in sync with the user's sequence of search terms.
600
+ .l-sub-section
601
+ :marked
602
+ We added the `debounceTime`, `distinctUntilChanged`, and `switchMap` operators to the RxJS `Observable` class
603
+ in `add-rxjs-operators` as [described above](#rxjs)
614
604
615
605
a#in-mem-web-api
616
606
.l-main-section
0 commit comments