Initial problem:
due to a connection to an external API creating new order (orders#create
) takes a few seconds and there is nothing I can do about in on my side. So I need to tell the user somehow, that page is actually doing something.
This is my js-code for validation:
$(document).ready(function(){
$('.submit-order').on('click', function(e){
var success = true;
$("div.text-danger").remove();
// a lot of validations, which set success to false if they fail
if (success === true) {
e.preventDefault();
$('.submit-order').find('input').val('Please hold on a little bit');
}
});
});
If I omit the e.preventDefault()
, then the (new) text displays only for a split second and then reverts back to "Proceed to payment".
I got from this and this SO answers, that I really need to include it. Then, the text stays "Please hold on a little bit". But, of course, now, no order is created.
How can I achieve a simple user informing while the backend is working? I stumbled upon this answer here, but is it really necessary to involve a complicated ajax call only for some text change?
Update #1: I don't really think, that my question is a duplicate of this one since the known solution does not work on my case.
My solution: Upon research, I stumbled upon 'data-disable-with' option, which was exactly what I needed. Now my submit button (Rails, Haml markup) looks like this:
.submit-order
=f.submit "Proceed to payment", data: { "disable-with" => "Please hold on a little bit"}, class: "btn btn-success-mod-uzhindoma margin-bottom-60"
Maybe it helps someone.