I'm using RxJS to listen to a form submit event, and submit it over AJAX (it's a weird bit of code, but I'm just experimenting with RxJS for now, so bear with me). What I want to do is only allow submissions when a request is not in progress.
My code so far looks like this (and is "working"):
Rx.DOM.submit(formEl)
.map(function(event){ event.preventDefault(); return event; })
.map(function(){ return getEmailAddress(formEl); })
.subscribe(function(email){
submitToMailChimp(url, email).done(showMailChimpThanks(orbitInstance)).fail(showMailChimpError);
});
My problem is I'm not sure how to implement something that will accomodate for this limitation (saw skipUntil but not quite sure how to handle that in this case). So my questions are: how do I (a) block submission until my Ajax request is done and (b) optimise my code to have the Ajax request at the end of the chain, so I can subscribe
to the result of it, instead of this weird hybrid mish-mash.
Final solution:
var submissions = Rx.DOM.submit(formEl);
submissions.subscribe(preventDefaultEvent);
var requests = submissions
.first()
.map(getEmailAddress(formEl))
.flatMap(submitToMailChimp(url))
.catch(showMailChimpError(submitButton))
.repeat();
requests.subscribe(showMailChimpThanks(orbitInstance, submitButton));