This repository has been archived by the owner on Dec 4, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 878
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added async form validation example for framework apis
- Loading branch information
1 parent
0d58e09
commit d6c0186
Showing
12 changed files
with
182 additions
and
17 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
public/docs/_examples/rxjs/ts/src/app/add-hero.component.html
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<h2>ADD HERO</h2> | ||
<form [formGroup]="form" (ngSubmit)="save(form.value)" *ngIf="!success"> | ||
<p> | ||
*Name: <input type="text" formControlName="name" #heroName><br> | ||
<span *ngIf="showErrors && form.hasError('required', ['name'])" class="error">Name is required</span> | ||
<span *ngIf="(form.get('name').statusChanges | async) === 'PENDING'" class="error">Checking if name is already taken</span> | ||
<span *ngIf="showErrors && form.hasError('taken', ['name'])" class="error">Hero name is already taken</span> | ||
</p> | ||
<p> | ||
<button type="submit" [disabled]="(form.statusChanges | async) === 'INVALID'">Save</button> | ||
</p> | ||
</form> | ||
|
||
<span *ngIf="success">The hero has been added</span> |
83 changes: 83 additions & 0 deletions
83
public/docs/_examples/rxjs/ts/src/app/add-hero.component.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// #docplaster | ||
// #docregion | ||
import 'rxjs/add/observable/of'; | ||
import 'rxjs/add/observable/fromEvent'; | ||
import 'rxjs/add/observable/merge'; | ||
import 'rxjs/add/operator/debounceTime'; | ||
import 'rxjs/add/operator/do'; | ||
import 'rxjs/add/operator/switchMap'; | ||
import 'rxjs/add/operator/take'; | ||
import { Component, OnInit, OnDestroy, AfterViewInit, ViewChild, ElementRef } from '@angular/core'; | ||
import { FormBuilder, FormGroup, FormControl, Validators } from '@angular/forms'; | ||
import { Observable } from 'rxjs/Observable'; | ||
import { Subject } from 'rxjs/Subject'; | ||
|
||
import { EventAggregatorService } from './event-aggregator.service'; | ||
import { HeroService } from './hero.service'; | ||
|
||
@Component({ | ||
moduleId: module.id, | ||
templateUrl: 'add-hero.component.html', | ||
styles: [ '.error { color: red }' ] | ||
}) | ||
export class AddHeroComponent implements OnInit, OnDestroy, AfterViewInit { | ||
@ViewChild('heroName', { read: ElementRef }) heroName: ElementRef; | ||
|
||
form: FormGroup; | ||
onDestroy$ = new Subject(); | ||
showErrors: boolean = false; | ||
success: boolean; | ||
|
||
constructor( | ||
private formBuilder: FormBuilder, | ||
private heroService: HeroService, | ||
private eventService: EventAggregatorService | ||
) {} | ||
|
||
ngOnInit() { | ||
this.form = this.formBuilder.group({ | ||
name: ['', [Validators.required], [(control: FormControl) => { | ||
return this.checkHeroName(control.value); | ||
}]] | ||
}); | ||
} | ||
|
||
checkHeroName(name: string) { | ||
return Observable.of(name) | ||
.switchMap(heroName => this.heroService.isNameAvailable(heroName)) | ||
.map(available => available ? null : { taken: true }); | ||
} | ||
|
||
ngAfterViewInit() { | ||
const controlBlur$ = Observable.fromEvent(this.heroName.nativeElement, 'blur'); | ||
|
||
Observable.merge( | ||
this.form.valueChanges, | ||
controlBlur$ | ||
) | ||
.debounceTime(300) | ||
.takeUntil(this.onDestroy$) | ||
.subscribe(() => this.checkErrors()); | ||
} | ||
|
||
checkErrors() { | ||
if (!this.form.valid) { | ||
this.showErrors = true; | ||
} | ||
} | ||
|
||
save(model: any) { | ||
this.heroService.addHero(model.name) | ||
.subscribe(() => { | ||
this.success = true; | ||
this.eventService.add({ | ||
type: 'hero', | ||
message: 'Hero Added' | ||
}); | ||
}); | ||
} | ||
|
||
ngOnDestroy() { | ||
this.onDestroy$.complete(); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// #docplaster | ||
// #docregion | ||
import 'rxjs/add/operator/map'; | ||
import 'rxjs/add/observable/of'; | ||
import 'rxjs/add/operator/catch'; | ||
// #docregion retry-import | ||
import 'rxjs/add/operator/retry'; | ||
// #enddocregion retry-import | ||
import { Injectable } from '@angular/core'; | ||
import { Http, Response, Headers } from '@angular/http'; | ||
import { Observable } from 'rxjs/Observable'; | ||
|
||
import { Hero } from './hero'; | ||
|
||
@Injectable() | ||
export class HeroService { | ||
private headers = new Headers({'Content-Type': 'application/json'}); | ||
private heroesUrl = 'api/heroes'; | ||
|
||
constructor( | ||
private http: Http | ||
) {} | ||
|
||
// #docregion getHeroes-failed | ||
getHeroes(fail?: boolean): Observable<Hero[]> { | ||
return this.http.get(`${this.heroesUrl}${fail ? '/failed' : ''}`) | ||
// #enddocregion getHeroes-failed | ||
.retry(3) | ||
.map(response => response.json().data as Hero[]) | ||
// #enddocregion getHeroes-failed | ||
.catch((error: any) => { | ||
console.log(`An error occurred: ${error}`); | ||
|
||
return Observable.of([]); | ||
}); | ||
// #docregion getHeroes-failed | ||
} | ||
|
||
addHero(name: string): Observable<Response> { | ||
return this.http | ||
.post(this.heroesUrl, JSON.stringify({name: name}), {headers: this.headers}); | ||
} | ||
|
||
isNameAvailable(name: string): Observable<boolean> { | ||
return this.http | ||
.get(`api/heroes/?name=${name}`) | ||
.map(response => response.json().data) | ||
.map(heroes => heroes.length === 0); | ||
} | ||
} |
This file contains 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
This file contains 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