| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  | // #docregion
 | 
					
						
							|  |  |  | import { Component, OnInit } from '@angular/core'; | 
					
						
							|  |  |  | import { Router }            from '@angular/router'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { Hero, HeroService } from '../model'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Component({ | 
					
						
							| 
									
										
										
										
											2016-09-25 18:51:54 -07:00
										 |  |  |   moduleId: module.id, | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |   selector: 'app-dashboard', | 
					
						
							| 
									
										
										
										
											2016-09-25 18:51:54 -07:00
										 |  |  |   templateUrl: 'dashboard.component.html', | 
					
						
							|  |  |  |   styleUrls: [ 'dashboard.component.css' ] | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  | }) | 
					
						
							|  |  |  | export class DashboardComponent implements OnInit { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   heroes: Hero[] = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // #docregion ctor
 | 
					
						
							|  |  |  |   constructor( | 
					
						
							|  |  |  |     private router: Router, | 
					
						
							|  |  |  |     private heroService: HeroService) { | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   // #enddocregion ctor
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ngOnInit() { | 
					
						
							|  |  |  |     this.heroService.getHeroes() | 
					
						
							|  |  |  |       .then(heroes => this.heroes = heroes.slice(1, 5)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // #docregion goto-detail
 | 
					
						
							|  |  |  |   gotoDetail(hero: Hero) { | 
					
						
							|  |  |  |     let url = `/heroes/${hero.id}`; | 
					
						
							|  |  |  |     this.router.navigateByUrl(url); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   // #enddocregion goto-detail
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   get title() { | 
					
						
							|  |  |  |     let cnt = this.heroes.length; | 
					
						
							|  |  |  |     return cnt === 0 ? 'No Heroes' : | 
					
						
							|  |  |  |       cnt === 1 ? 'Top Hero' :  `Top ${cnt} Heroes`; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |