|
| 1 | +import {Component, OnInit} from 'angular2/core'; |
| 2 | +import {<%= classifiedModuleName %>, <%= classifiedModuleName %>Service} from './<%= dasherizedModuleName %>.service'; |
| 3 | +import {RouteParams, Router} from 'angular2/router'; |
| 4 | +import {CanDeactivate, ComponentInstruction} from 'angular2/router'; |
| 5 | + |
| 6 | +@Component({ |
| 7 | + templateUrl: 'app/<%= dasherizedModuleName %>/<%= dasherizedModuleName %>-detail.component.html', |
| 8 | + styleUrls: ['app/<%= dasherizedModuleName %>/<%= dasherizedModuleName %>-detail.component.css'] |
| 9 | +}) |
| 10 | +export class <%= classifiedModuleName %>DetailComponent implements OnInit, CanDeactivate { |
| 11 | + |
| 12 | + <%= camelizedModuleName %>: <%= classifiedModuleName %>; |
| 13 | + editName: string; |
| 14 | + |
| 15 | + constructor( |
| 16 | + private _service: <%= classifiedModuleName %>Service, |
| 17 | + private _router: Router, |
| 18 | + private _routeParams: RouteParams |
| 19 | + ) { } |
| 20 | + |
| 21 | + ngOnInit() { |
| 22 | + let id = +this._routeParams.get('id'); |
| 23 | + this._service.get(id).then(<%= camelizedModuleName %> => { |
| 24 | + if (<%= camelizedModuleName %>) { |
| 25 | + this.editName = <%= camelizedModuleName %>.name; |
| 26 | + this.<%= camelizedModuleName %> = <%= camelizedModuleName %>; |
| 27 | + } else { |
| 28 | + this.gotoList(); |
| 29 | + } |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction): any { |
| 34 | + if (!this.<%= camelizedModuleName %> || this.<%= camelizedModuleName %>.name === this.editName) { |
| 35 | + return true; |
| 36 | + } |
| 37 | + |
| 38 | + return new Promise<boolean>((resolve, reject) => resolve(window.confirm('Discard changes?'))); |
| 39 | + } |
| 40 | + |
| 41 | + cancel() { |
| 42 | + this.editName = this.<%= camelizedModuleName %>.name; |
| 43 | + this.gotoList(); |
| 44 | + } |
| 45 | + |
| 46 | + save() { |
| 47 | + this.<%= camelizedModuleName %>.name = this.editName; |
| 48 | + this.gotoList(); |
| 49 | + } |
| 50 | + |
| 51 | + gotoList() { |
| 52 | + this._router.navigate(['<%= classifiedModuleName %>List']); |
| 53 | + } |
| 54 | +} |
0 commit comments