Skip to content

Instantly share code, notes, and snippets.

@chenkie
Last active April 28, 2016 19:56
Show Gist options
  • Save chenkie/2fa24c1f4a9ef5ebe24715c4fa012d57 to your computer and use it in GitHub Desktop.
Save chenkie/2fa24c1f4a9ef5ebe24715c4fa012d57 to your computer and use it in GitHub Desktop.
import {Http, HTTP_PROVIDERS} from 'angular2/http';
import {Injector} from 'angular2/core'
import {Control} from 'angular2/common';
import 'rxjs/Rx';
export class UsernameEmailValidator {
constructor() {}
static checkUser(control: Control) {
let injector = Injector.resolveAndCreate([HTTP_PROVIDERS]);
var http = injector.get(Http);
control.valueChanges
.debounceTime(500)
.flatMap(value => http.post('http://localhost:3000/api/users/check', JSON.stringify({ username: value })))
.subscribe(
data => {
return null;
},
error => {
return {'usernameTaken': true};
}
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment