| 
									
										
										
										
											2016-05-13 13:32:54 -07:00
										 |  |  | extends ../../../ts/latest/guide/server-communication.jade | 
					
						
							| 
									
										
										
										
											2016-02-29 21:25:28 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-13 13:32:54 -07:00
										 |  |  | block includes | 
					
						
							|  |  |  |   include ../_util-fns | 
					
						
							| 
									
										
										
										
											2016-05-17 21:25:41 -07:00
										 |  |  |   - var _Http = 'BrowserClient'; | 
					
						
							|  |  |  |   - var _Angular_Http = 'Dart <code>BrowserClient</code>' | 
					
						
							|  |  |  |   - var _httpUrl = 'https://pub.dartlang.org/packages/http' | 
					
						
							| 
									
										
										
										
											2016-05-20 17:14:37 -07:00
										 |  |  |   - var _Angular_http_library = 'Dart <a href="' + _httpUrl + '"><b>http</b></a> library' | 
					
						
							| 
									
										
										
										
											2016-05-17 21:25:41 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | block demos-list | 
					
						
							| 
									
										
										
										
											2016-05-20 17:14:37 -07:00
										 |  |  |   li #[a(href="#http-client") HTTP client: Tour of Heroes] | 
					
						
							|  |  |  |   li #[a(href="#cors") JSONP client: Wikipedia to fetch data from a service that does not support CORS] #[b (under development)] | 
					
						
							| 
									
										
										
										
											2016-05-17 21:25:41 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | block rxjs-import | 
					
						
							|  |  |  |   //- N/A | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | block http-client | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  | block system-config-of-http | 
					
						
							|  |  |  |   //- N/A | 
					
						
							| 
									
										
										
										
											2016-02-29 21:25:28 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-13 13:32:54 -07:00
										 |  |  | block http-providers | 
					
						
							| 
									
										
										
										
											2016-05-17 21:25:41 -07:00
										 |  |  |   :marked | 
					
						
							|  |  |  |     Actually, it is unnecessary to include `BrowserClient` in the list of providers. | 
					
						
							|  |  |  |     ***But*** as is mentioned in the *Angular 2 Dart Transformer* [wiki page][ng2dtri], | 
					
						
							|  |  |  |     the template compiler _generates_ dependency injection code, hence all the | 
					
						
							|  |  |  |     identifiers used in DI have to be collected by the Angular 2 transformer | 
					
						
							|  |  |  |     so that the libraries containing these identifiers can be transformed. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Unless special steps are taken, Dart libraries like `http` | 
					
						
							|  |  |  |     are not transformed. To ensure that the `BrowserClient` identifier is available | 
					
						
							|  |  |  |     for DI, we must add a `resolved_identifiers` parameter to the `angular2` | 
					
						
							|  |  |  |     transformer in `pubspec.yaml`: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     [ng2dtri]: https://github.com/angular/angular/wiki/Angular-2-Dart-Transformer#resolved_identifiers | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   - var stylePattern = { pnk: /(resolved_identifiers:|Browser.*)/gm, otl: /(- angular2:)|(transformers:)/g }; | 
					
						
							|  |  |  |   +makeExcerpt('pubspec.yaml', 'transformers', 'pubspec.yaml (transformers)', stylePattern) | 
					
						
							| 
									
										
										
										
											2016-02-29 21:25:28 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-13 13:32:54 -07:00
										 |  |  | block getheroes-and-addhero | 
					
						
							|  |  |  |   :marked | 
					
						
							|  |  |  |     The hero service `getHeroes()` and `addHero()` asynchronous methods return the | 
					
						
							|  |  |  |     [`Future`](https://api.dartlang.org/stable/1.16.0/dart-async/Future-class.html) | 
					
						
							|  |  |  |     values of the current hero list and the newly added hero, | 
					
						
							|  |  |  |     respectively. The hero list component methods of the same name specifying | 
					
						
							|  |  |  |     the actions to be taken when the asynchronous method calls succeed or fail. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     For more information about `Future`s, consult any one | 
					
						
							|  |  |  |     of the [articles](https://www.dartlang.org/articles/) on asynchronous | 
					
						
							|  |  |  |     programming in Dart, or the tutorial on  | 
					
						
							|  |  |  |     [_Asynchronous Programming: Futures_](https://www.dartlang.org/docs/tutorials/futures/). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | block rxjs | 
					
						
							|  |  |  |   //- N/A | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | block parse-json | 
					
						
							|  |  |  |   :marked | 
					
						
							|  |  |  |     The response data are in JSON string form. | 
					
						
							| 
									
										
										
										
											2016-05-17 21:25:41 -07:00
										 |  |  |     We must parse that string into Objects which we do by calling | 
					
						
							| 
									
										
										
										
											2016-05-13 13:32:54 -07:00
										 |  |  |     the `JSON.decode()` method from the `dart:convert` library. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | block error-handling | 
					
						
							|  |  |  |   //- TODO: describe `_handleError`? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | block hlc-error-handling | 
					
						
							|  |  |  |   :marked | 
					
						
							|  |  |  |     Back in the `HeroListComponent`, we wrapped our call to | 
					
						
							|  |  |  |     `#{_priv}heroService.getHeroes()` in a `try` clause. When an exception is | 
					
						
							|  |  |  |     caught, the `errorMessage` variable — which we've bound conditionally in the | 
					
						
							|  |  |  |     template — gets assigned to. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | block hero-list-comp-add-hero | 
					
						
							|  |  |  |   :marked | 
					
						
							|  |  |  |     Back in the `HeroListComponent`, we see that *its* `addHero()` | 
					
						
							|  |  |  |     awaits for the *service's* asynchronous `addHero()` to return, and when it does, | 
					
						
							|  |  |  |     the new hero is added to the `heroes` list for presentation to the user. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | block promises | 
					
						
							|  |  |  |   //- N/A | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | block wikipedia-jsonp+ | 
					
						
							|  |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2016-05-17 21:25:41 -07:00
										 |  |  |     Wikipedia offers a modern `CORS` API and a legacy `JSONP` search API. | 
					
						
							| 
									
										
										
										
											2016-05-13 13:32:54 -07:00
										 |  |  |   .alert.is-important | 
					
						
							|  |  |  |     :marked | 
					
						
							|  |  |  |       The remaining content of this section is coming soon. | 
					
						
							|  |  |  |       In the meantime, consult the  | 
					
						
							|  |  |  |       [example sources](https://github.com/angular-examples/server-communication) | 
					
						
							|  |  |  |       to see how to access Wikipedia via its `JSONP` API. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | block redirect-to-web-api | 
					
						
							|  |  |  |   :marked | 
					
						
							|  |  |  |     To achieve this, we have Angular inject an in-memory web API server | 
					
						
							|  |  |  |     instance as a provider for the `BrowserClient`. This is possible because | 
					
						
							| 
									
										
										
										
											2016-05-17 21:25:41 -07:00
										 |  |  |     the in-memory web API server class extends `BrowserClient`. |