Skip to content

Instantly share code, notes, and snippets.

@ipiyer
Last active February 7, 2016 02:54
Show Gist options
  • Save ipiyer/c4ef611994b4d6550c80 to your computer and use it in GitHub Desktop.
Save ipiyer/c4ef611994b4d6550c80 to your computer and use it in GitHub Desktop.
FRP username validation
// username is a input element in the dom
let userNameVal = Kefir.fromEvents(username, "blur")
.map(e => $(e.target)
.val());
let kUsernameValidation = username => {
try {
if (usernameValidation(username)) {
return username;
}
} catch (e) {
return Kefir.constantError(e.message);
}
}
let validUsername = userNameVal
.flatMapLatest(kUsernameValidation)
.onError(function(error) {
showErrors(helpText, error); // How do i end the stream here? is there a better way to write this?
});
let usernameAvail = validUsername
.flatMapLatest(isUserNameTaken) // does xhr to server
.map(function(value) {
hideErrors(helpText);
// username exists;
if (value) {
showInputUnAvailable(username, "username is already taken.")
} else {
showInputAvailable(username);
}
return value;
}).onError(function() {
window.alert("something went wrong");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment