| 
									
										
										
										
											2015-11-25 18:01:44 +01:00
										 |  |  | // #docregion
 | 
					
						
							| 
									
										
										
										
											2016-05-03 14:06:32 +02:00
										 |  |  | import { Component, EventEmitter, Input, Output } from '@angular/core'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { RestoreService } from './restore.service'; | 
					
						
							|  |  |  | import { Hero } from './hero'; | 
					
						
							| 
									
										
										
										
											2015-11-25 18:01:44 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'hero-editor', | 
					
						
							|  |  |  |   // #docregion providers
 | 
					
						
							|  |  |  |   providers: [RestoreService], | 
					
						
							|  |  |  |   // #enddocregion providers
 | 
					
						
							|  |  |  |   template: `
 | 
					
						
							|  |  |  |     <div> | 
					
						
							|  |  |  |       <span>Name:</span> | 
					
						
							| 
									
										
										
										
											2015-12-11 13:18:37 -08:00
										 |  |  |       <input [(ngModel)]="hero.name"/> | 
					
						
							| 
									
										
										
										
											2015-11-25 18:01:44 +01:00
										 |  |  |       <div> | 
					
						
							|  |  |  |         <button (click)="onSaved()">save</button> | 
					
						
							|  |  |  |         <button (click)="onCanceled()">cancel</button> | 
					
						
							|  |  |  |       </div> | 
					
						
							|  |  |  |     </div>`
 | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class HeroEditorComponent { | 
					
						
							| 
									
										
										
										
											2016-11-19 06:29:57 +00:00
										 |  |  |   @Output() canceled = new EventEmitter<Hero>(); | 
					
						
							|  |  |  |   @Output() saved = new EventEmitter<Hero>(); | 
					
						
							| 
									
										
										
										
											2015-11-25 18:01:44 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   constructor(private restoreService: RestoreService<Hero>) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @Input() | 
					
						
							|  |  |  |   set hero (hero: Hero) { | 
					
						
							|  |  |  |     this.restoreService.setItem(hero); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   get hero () { | 
					
						
							|  |  |  |     return this.restoreService.getItem(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   onSaved () { | 
					
						
							| 
									
										
										
										
											2016-11-19 06:29:57 +00:00
										 |  |  |     this.saved.emit(this.restoreService.getItem()); | 
					
						
							| 
									
										
										
										
											2015-11-25 18:01:44 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   onCanceled () { | 
					
						
							|  |  |  |     this.hero = this.restoreService.restoreItem(); | 
					
						
							| 
									
										
										
										
											2016-11-19 06:29:57 +00:00
										 |  |  |     this.canceled.emit(this.hero); | 
					
						
							| 
									
										
										
										
											2015-11-25 18:01:44 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | // #enddocregion
 |