| 
									
										
										
										
											2016-04-09 00:18:37 -04:00
										 |  |  | // #docregion
 | 
					
						
							|  |  |  | import { Component, OnInit } from '@angular/core'; | 
					
						
							|  |  |  | import { Router }            from '@angular/router-deprecated'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { Hero }                from './hero'; | 
					
						
							|  |  |  | import { HeroService }         from './hero.service'; | 
					
						
							| 
									
										
										
										
											2016-05-19 19:00:20 -07:00
										 |  |  | // #docregion hero-detail-component
 | 
					
						
							|  |  |  | import { HeroDetailComponent } from './hero-detail.component'; | 
					
						
							| 
									
										
										
										
											2016-04-09 00:18:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'my-heroes', | 
					
						
							|  |  |  |   templateUrl: 'app/heroes.component.html', | 
					
						
							| 
									
										
										
										
											2016-05-19 19:00:20 -07:00
										 |  |  |   styleUrls:  ['app/heroes.component.css'], | 
					
						
							|  |  |  |   directives: [HeroDetailComponent] | 
					
						
							| 
									
										
										
										
											2016-04-09 00:18:37 -04:00
										 |  |  | }) | 
					
						
							| 
									
										
										
										
											2016-05-19 19:00:20 -07:00
										 |  |  | // #enddocregion hero-detail-component
 | 
					
						
							| 
									
										
										
										
											2016-04-09 00:18:37 -04:00
										 |  |  | export class HeroesComponent implements OnInit { | 
					
						
							|  |  |  |   heroes: Hero[]; | 
					
						
							|  |  |  |   selectedHero: Hero; | 
					
						
							|  |  |  |   addingHero = false; | 
					
						
							|  |  |  |   error: any; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   constructor( | 
					
						
							| 
									
										
										
										
											2016-05-23 10:02:17 +02:00
										 |  |  |     private router: Router, | 
					
						
							|  |  |  |     private heroService: HeroService) { } | 
					
						
							| 
									
										
										
										
											2016-04-09 00:18:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   getHeroes() { | 
					
						
							| 
									
										
										
										
											2016-05-23 10:02:17 +02:00
										 |  |  |     this.heroService | 
					
						
							| 
									
										
										
										
											2016-04-09 00:18:37 -04:00
										 |  |  |         .getHeroes() | 
					
						
							|  |  |  |         .then(heroes => this.heroes = heroes) | 
					
						
							|  |  |  |         .catch(error => this.error = error); // TODO: Display error message
 | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-19 19:00:20 -07:00
										 |  |  |   // #docregion add
 | 
					
						
							| 
									
										
										
										
											2016-04-09 00:18:37 -04:00
										 |  |  |   addHero() { | 
					
						
							|  |  |  |     this.addingHero = true; | 
					
						
							|  |  |  |     this.selectedHero = null; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   close(savedHero: Hero) { | 
					
						
							|  |  |  |     this.addingHero = false; | 
					
						
							|  |  |  |     if (savedHero) { this.getHeroes(); } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-05-19 19:00:20 -07:00
										 |  |  |   // #enddocregion add
 | 
					
						
							| 
									
										
										
										
											2016-04-09 00:18:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // #docregion delete
 | 
					
						
							|  |  |  |   delete(hero: Hero, event: any) { | 
					
						
							|  |  |  |     event.stopPropagation(); | 
					
						
							| 
									
										
										
										
											2016-05-23 10:02:17 +02:00
										 |  |  |     this.heroService | 
					
						
							| 
									
										
										
										
											2016-04-09 00:18:37 -04:00
										 |  |  |         .delete(hero) | 
					
						
							|  |  |  |         .then(res => { | 
					
						
							| 
									
										
										
										
											2016-05-19 21:50:26 -07:00
										 |  |  |           this.heroes = this.heroes.filter(h => h !== hero); | 
					
						
							|  |  |  |           if (this.selectedHero === hero) { this.selectedHero = null; } | 
					
						
							| 
									
										
										
										
											2016-04-09 00:18:37 -04:00
										 |  |  |         }) | 
					
						
							|  |  |  |         .catch(error => this.error = error); // TODO: Display error message
 | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   // #enddocregion delete
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ngOnInit() { | 
					
						
							|  |  |  |     this.getHeroes(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   onSelect(hero: Hero) { | 
					
						
							|  |  |  |     this.selectedHero = hero; | 
					
						
							|  |  |  |     this.addingHero = false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   gotoDetail() { | 
					
						
							| 
									
										
										
										
											2016-05-23 10:02:17 +02:00
										 |  |  |     this.router.navigate(['HeroDetail', { id: this.selectedHero.id }]); | 
					
						
							| 
									
										
										
										
											2016-04-09 00:18:37 -04:00
										 |  |  |   } | 
					
						
							|  |  |  | } |