| 
									
										
										
										
											2016-04-09 00:18:37 -04:00
										 |  |  | // #docplaster
 | 
					
						
							| 
									
										
										
										
											2016-06-11 19:55:43 +02:00
										 |  |  | // #docregion, variables-imports
 | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  | import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2016-06-11 19:55:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | // #enddocregion variables-imports
 | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  | import { ActivatedRoute, Params } from '@angular/router'; | 
					
						
							| 
									
										
										
										
											2016-04-09 00:18:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | import { Hero }        from './hero'; | 
					
						
							|  |  |  | import { HeroService } from './hero.service'; | 
					
						
							| 
									
										
										
										
											2016-05-23 10:02:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-09 00:18:37 -04:00
										 |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'my-hero-detail', | 
					
						
							|  |  |  |   templateUrl: 'app/hero-detail.component.html', | 
					
						
							|  |  |  |   styleUrls: ['app/hero-detail.component.css'] | 
					
						
							|  |  |  | }) | 
					
						
							| 
									
										
										
										
											2016-06-11 19:55:43 +02:00
										 |  |  | // #docregion variables-imports
 | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  | export class HeroDetailComponent implements OnInit { | 
					
						
							| 
									
										
										
										
											2016-04-09 00:18:37 -04:00
										 |  |  |   @Input() hero: Hero; | 
					
						
							|  |  |  |   @Output() close = new EventEmitter(); | 
					
						
							|  |  |  |   error: any; | 
					
						
							|  |  |  |   navigated = false; // true if navigated here
 | 
					
						
							| 
									
										
										
										
											2016-06-11 19:55:43 +02:00
										 |  |  |   // #enddocregion variables-imports
 | 
					
						
							| 
									
										
										
										
											2016-04-09 00:18:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   constructor( | 
					
						
							| 
									
										
										
										
											2016-05-23 10:02:17 +02:00
										 |  |  |     private heroService: HeroService, | 
					
						
							| 
									
										
										
										
											2016-06-26 12:13:44 -05:00
										 |  |  |     private route: ActivatedRoute) { | 
					
						
							| 
									
										
										
										
											2016-04-09 00:18:37 -04:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // #docregion ngOnInit
 | 
					
						
							| 
									
										
										
										
											2016-07-27 15:00:59 +02:00
										 |  |  |   ngOnInit(): void { | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  |     this.route.params.forEach((params: Params) => { | 
					
						
							| 
									
										
										
										
											2016-06-26 12:13:44 -05:00
										 |  |  |       if (params['id'] !== undefined) { | 
					
						
							|  |  |  |         let id = +params['id']; | 
					
						
							|  |  |  |         this.navigated = true; | 
					
						
							|  |  |  |         this.heroService.getHero(id) | 
					
						
							|  |  |  |             .then(hero => this.hero = hero); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         this.navigated = false; | 
					
						
							|  |  |  |         this.hero = new Hero(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-04-09 00:18:37 -04:00
										 |  |  |   } | 
					
						
							|  |  |  |   // #enddocregion ngOnInit
 | 
					
						
							| 
									
										
										
										
											2016-06-26 12:13:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-09 00:18:37 -04:00
										 |  |  |   // #docregion save
 | 
					
						
							| 
									
										
										
										
											2016-07-27 15:00:59 +02:00
										 |  |  |   save(): void { | 
					
						
							| 
									
										
										
										
											2016-05-23 10:02:17 +02:00
										 |  |  |     this.heroService | 
					
						
							| 
									
										
										
										
											2016-04-09 00:18:37 -04:00
										 |  |  |         .save(this.hero) | 
					
						
							|  |  |  |         .then(hero => { | 
					
						
							|  |  |  |           this.hero = hero; // saved hero, w/ id if new
 | 
					
						
							|  |  |  |           this.goBack(hero); | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |         .catch(error => this.error = error); // TODO: Display error message
 | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   // #enddocregion save
 | 
					
						
							| 
									
										
										
										
											2016-06-27 11:23:27 -07:00
										 |  |  |   // #docregion goBack
 | 
					
						
							| 
									
										
										
										
											2016-07-27 15:00:59 +02:00
										 |  |  |   goBack(savedHero: Hero = null): void { | 
					
						
							| 
									
										
										
										
											2016-04-09 00:18:37 -04:00
										 |  |  |     this.close.emit(savedHero); | 
					
						
							|  |  |  |     if (this.navigated) { window.history.back(); } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-06-27 11:23:27 -07:00
										 |  |  |   // #enddocregion goBack
 | 
					
						
							| 
									
										
										
										
											2016-04-09 00:18:37 -04:00
										 |  |  | } |