2016-04-09 00:18:37 -04:00
|
|
|
// #docplaster
|
2016-06-11 13:55:43 -04:00
|
|
|
// #docregion, variables-imports
|
2016-08-09 12:38:25 -04:00
|
|
|
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
|
2016-06-11 13:55:43 -04:00
|
|
|
|
|
|
|
// #enddocregion variables-imports
|
2016-08-09 12:38:25 -04: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 04:02:17 -04: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 13:55:43 -04:00
|
|
|
// #docregion variables-imports
|
2016-08-09 12:38:25 -04: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 13:55:43 -04:00
|
|
|
// #enddocregion variables-imports
|
2016-04-09 00:18:37 -04:00
|
|
|
|
|
|
|
constructor(
|
2016-05-23 04:02:17 -04:00
|
|
|
private heroService: HeroService,
|
2016-06-26 13:13:44 -04:00
|
|
|
private route: ActivatedRoute) {
|
2016-04-09 00:18:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// #docregion ngOnInit
|
|
|
|
ngOnInit() {
|
2016-08-09 12:38:25 -04:00
|
|
|
this.route.params.forEach((params: Params) => {
|
2016-06-26 13:13:44 -04: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 13:13:44 -04:00
|
|
|
|
2016-04-09 00:18:37 -04:00
|
|
|
// #docregion save
|
|
|
|
save() {
|
2016-05-23 04:02:17 -04: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 14:23:27 -04:00
|
|
|
// #docregion goBack
|
2016-04-09 00:18:37 -04:00
|
|
|
goBack(savedHero: Hero = null) {
|
|
|
|
this.close.emit(savedHero);
|
|
|
|
if (this.navigated) { window.history.back(); }
|
|
|
|
}
|
2016-06-27 14:23:27 -04:00
|
|
|
// #enddocregion goBack
|
2016-04-09 00:18:37 -04:00
|
|
|
}
|