|
| 1 | +// #docplaster |
| 2 | +// #docregion , v2 |
| 3 | +import 'dart:async'; |
| 4 | +import 'dart:html'; |
| 5 | + |
| 6 | +// #docregion import-oninit |
| 7 | +import 'package:angular2/core.dart'; |
| 8 | +// #enddocregion import-oninit |
| 9 | +// #docregion import-route-params |
| 10 | +import 'package:angular2/router.dart'; |
| 11 | +// #enddocregion import-route-params |
| 12 | + |
| 13 | +import 'hero.dart'; |
| 14 | +// #docregion import-hero-service |
| 15 | +import 'hero_service.dart'; |
| 16 | +// #enddocregion import-hero-service |
| 17 | + |
| 18 | +// #docregion extract-template |
| 19 | +@Component( |
| 20 | + selector: 'my-hero-detail', |
| 21 | + // #docregion template-url |
| 22 | + templateUrl: 'hero_detail_component.html', |
| 23 | + // #enddocregion template-url, v2 |
| 24 | + styleUrls: const ['hero_detail_component.css'] |
| 25 | + // #docregion v2 |
| 26 | + ) |
| 27 | +// #enddocregion extract-template |
| 28 | +// #docregion implement |
| 29 | +class HeroDetailComponent implements OnInit { |
| 30 | + // #enddocregion implement |
| 31 | + Hero hero; |
| 32 | + // #docregion ctor |
| 33 | + final HeroService _heroService; |
| 34 | + final RouteParams _routeParams; |
| 35 | + |
| 36 | + HeroDetailComponent(this._heroService, this._routeParams); |
| 37 | + // #enddocregion ctor |
| 38 | + |
| 39 | + // #docregion ng-oninit |
| 40 | + Future<Null> ngOnInit() async { |
| 41 | + // #docregion get-id |
| 42 | + var idString = _routeParams.get('id'); |
| 43 | + var id = int.parse(idString, onError: (_) => null); |
| 44 | + // #enddocregion get-id |
| 45 | + if (id != null) hero = await (_heroService.getHero(id)); |
| 46 | + } |
| 47 | + // #enddocregion ng-oninit |
| 48 | + |
| 49 | + // #docregion save |
| 50 | + Future<Null> save() async { |
| 51 | + await _heroService.save(hero); |
| 52 | + goBack(); |
| 53 | + } |
| 54 | + // #enddocregion save |
| 55 | + |
| 56 | + // #docregion go-back |
| 57 | + void goBack() { |
| 58 | + window.history.back(); |
| 59 | + } |
| 60 | + // #enddocregion go-back |
| 61 | +} |
0 commit comments