Skip to content

Instantly share code, notes, and snippets.

@AndAShape
Created April 3, 2015 14:09
Show Gist options
  • Save AndAShape/1a32da85ed6001d62986 to your computer and use it in GitHub Desktop.
Save AndAShape/1a32da85ed6001d62986 to your computer and use it in GitHub Desktop.
AJAX request with Bacon.js
$('#authenticateCont form')
.asEventStream('submit')
.map(function(e) {
e.preventDefault();
return $(e.target);
})
.filter(function(form) {
return form.valid();
})
.flatMapLatest(function(form) {
$('#authenticateCont :submit')
.attr('disabled', 'disabled')
.val('REALLY BUSY');
$('#lblResponse').hide();
return Bacon.fromPromise($.post(
form.attr('action') + '?r=' + Math.random(),
form.serializeArray()));
})
.onValue(function(r) {
if (r.userId === 0) {
$('#authenticateCont :submit').removeAttr('disabled').val('LET\'S DO THIS');
$('#lblResponse').html('Sorry, please try again.').show();
} else {
location.reload();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment