Created
April 3, 2015 14:09
-
-
Save AndAShape1/1a32da85ed6001d62986 to your computer and use it in GitHub Desktop.
AJAX request with Bacon.js
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
$('#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