|
| 1 | +// #docregion |
| 2 | +import { Component, OnInit } from '@angular/core'; |
| 3 | +import { Router } from '@angular/router'; |
| 4 | + |
| 5 | +// #docregion import-apollo |
| 6 | +//.. |
| 7 | +import { Angular2Apollo, ApolloQueryObservable } from 'angular2-apollo'; |
| 8 | +//.. |
| 9 | +// #enddocregion import-apollo |
| 10 | +// #docregion import-graphql-tag |
| 11 | +//.. |
| 12 | +import gql from 'graphql-tag'; |
| 13 | +//.. |
| 14 | +// #enddocregion import-graphql-tag |
| 15 | +import { ApolloQueryResult } from 'apollo-client'; |
| 16 | +import { Hero } from './hero'; |
| 17 | + |
| 18 | +@Component({ |
| 19 | + moduleId: module.id, |
| 20 | + selector: 'my-heroes', |
| 21 | + templateUrl: 'heroes.component.html', |
| 22 | + styleUrls: [ 'heroes.component.css' ] |
| 23 | +}) |
| 24 | +export class HeroesComponent implements OnInit { |
| 25 | + // #docregion this-heroes |
| 26 | + heroes: Hero[]; |
| 27 | + selectedHero: Hero; |
| 28 | + // #enddocregion this-heroes |
| 29 | + |
| 30 | +// #docregion inject-apollo |
| 31 | + constructor( |
| 32 | + private apollo: Angular2Apollo, |
| 33 | + private router: Router) { } |
| 34 | + // #enddocregion inject-apollo |
| 35 | + |
| 36 | + // #docregion query-heroes |
| 37 | + getHeroes(): void { |
| 38 | + this.apollo.watchQuery({ |
| 39 | + query: gql` |
| 40 | + query allHeroes { |
| 41 | + heroes { |
| 42 | + id |
| 43 | + name |
| 44 | + } |
| 45 | + } |
| 46 | + `, |
| 47 | + }).subscribe((queryResult: ApolloQueryResult) => { |
| 48 | + this.heroes = queryResult.data.heroes; |
| 49 | + }); |
| 50 | + // #enddocregion query-heroes |
| 51 | + } |
| 52 | + |
| 53 | + // #docregion add |
| 54 | + add(name: string): void { |
| 55 | + } |
| 56 | + // #enddocregion add |
| 57 | + |
| 58 | + // #docregion delete |
| 59 | + delete(hero: Hero): void { |
| 60 | + // this.heroService |
| 61 | + // .delete(hero.id) |
| 62 | + // .then(() => { |
| 63 | + // this.heroes = this.heroes.filter(h => h !== hero); |
| 64 | + // if (this.selectedHero === hero) { this.selectedHero = null; } |
| 65 | + // }); |
| 66 | + } |
| 67 | + // #enddocregion delete |
| 68 | + |
| 69 | + ngOnInit(): void { |
| 70 | + this.getHeroes(); |
| 71 | + } |
| 72 | + |
| 73 | + onSelect(hero: Hero): void { |
| 74 | + this.selectedHero = hero; |
| 75 | + } |
| 76 | + |
| 77 | + gotoDetail(): void { |
| 78 | + this.router.navigate(['/detail', this.selectedHero.id]); |
| 79 | + } |
| 80 | +} |
0 commit comments