* docs(toh-6/dart): first edition of prose and example code NOTE: this PR depends on #1686. Dart prose and example match TS except that: - No child-to-parent event emission occurs. - Support for Add Hero is added as an unconditional feature of the Heroes view. - http `_post` takes only a name - http `delete` takes only a hero id. - The Dart in-memory-data-service has been dropped in favor of an implementation based on the "standard" `http.testing.MockClient` class. * post-review changes
		
			
				
	
	
		
			70 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
			
		
		
	
	
			70 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
// #docplaster
 | 
						|
// #docregion
 | 
						|
import 'dart:async';
 | 
						|
 | 
						|
import 'package:angular2/core.dart';
 | 
						|
import 'package:angular2/router.dart';
 | 
						|
 | 
						|
import 'hero.dart';
 | 
						|
import 'hero_detail_component.dart';
 | 
						|
import 'hero_service.dart';
 | 
						|
 | 
						|
@Component(
 | 
						|
    selector: 'my-heroes',
 | 
						|
    templateUrl: 'heroes_component.html',
 | 
						|
    styleUrls: const ['heroes_component.css'],
 | 
						|
    directives: const [HeroDetailComponent])
 | 
						|
class HeroesComponent implements OnInit {
 | 
						|
  final Router _router;
 | 
						|
  final HeroService _heroService;
 | 
						|
  List<Hero> heroes;
 | 
						|
  Hero selectedHero;
 | 
						|
  // #docregion error
 | 
						|
  String errorMessage;
 | 
						|
  // #enddocregion error
 | 
						|
 | 
						|
  HeroesComponent(this._heroService, this._router);
 | 
						|
 | 
						|
  // #docregion addHero
 | 
						|
  Future<Null> addHero(String name) async {
 | 
						|
    name = name.trim();
 | 
						|
    if (name.isEmpty) return;
 | 
						|
    try {
 | 
						|
      heroes.add(await _heroService.save(name));
 | 
						|
    } catch (e) {
 | 
						|
      errorMessage = e.toString();
 | 
						|
    }
 | 
						|
  }
 | 
						|
  // #enddocregion addHero
 | 
						|
 | 
						|
  // #docregion deleteHero
 | 
						|
  Future<Null> deleteHero(int id, event) async {
 | 
						|
    try {
 | 
						|
      event.stopPropagation();
 | 
						|
      await _heroService.delete(id);
 | 
						|
      heroes.removeWhere((hero) => hero.id == id);
 | 
						|
      if (selectedHero?.id == id) selectedHero = null;
 | 
						|
    } catch (e) {
 | 
						|
      errorMessage = e.toString();
 | 
						|
    }
 | 
						|
  }
 | 
						|
  // #enddocregion deleteHero
 | 
						|
 | 
						|
  Future<Null> getHeroes() async {
 | 
						|
    heroes = await _heroService.getHeroes();
 | 
						|
  }
 | 
						|
 | 
						|
  void ngOnInit() {
 | 
						|
    getHeroes();
 | 
						|
  }
 | 
						|
 | 
						|
  void onSelect(Hero hero) {
 | 
						|
    selectedHero = hero;
 | 
						|
  }
 | 
						|
 | 
						|
  Future<Null> gotoDetail() => _router.navigate([
 | 
						|
        'HeroDetail',
 | 
						|
        {'id': selectedHero.id.toString()}
 | 
						|
      ]);
 | 
						|
}
 |