Created
December 4, 2018 07:19
-
-
Save LayZeeDK/bbb2636ea4107bcafb4a68e0ead1d1de to your computer and use it in GitHub Desktop.
Hero search: Presentational component model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
ChangeDetectionStrategy, | |
Component, | |
EventEmitter, | |
Input, | |
OnDestroy, | |
OnInit, | |
Output, | |
} from '@angular/core'; | |
import { Subject } from 'rxjs'; | |
import { takeUntil } from 'rxjs/operators'; | |
import { Hero } from '../hero'; | |
import { HeroSearchPresenter } from './hero-search.presenter'; | |
@Component({ | |
selector: 'app-hero-search-ui', | |
templateUrl: './hero-search.component.html', | |
styleUrls: ['./hero-search.component.css'], | |
changeDetection: ChangeDetectionStrategy.OnPush, | |
providers: [HeroSearchPresenter], | |
}) | |
export class HeroSearchComponent implements OnDestroy, OnInit { | |
@Input() heroes: Hero[]; | |
@Input() title: string; | |
@Output() search: EventEmitter<string> = new EventEmitter(); | |
private destroy: Subject<void> = new Subject(); | |
constructor(private presenter: HeroSearchPresenter) {} | |
ngOnInit(): void { | |
this.presenter.searchTerms$.pipe( | |
// complete when component is destroyed | |
takeUntil(this.destroy), | |
).subscribe(term => this.search.emit(term)); | |
} | |
ngOnDestroy(): void { | |
this.destroy.next(); | |
this.destroy.complete(); | |
} | |
searchFor(term: string): void { | |
this.presenter.search(term); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment