JangoSteve / remotipart

Rails jQuery file uploads via standard Rails "remote: true" forms.
http://os.alfajango.com/remotipart
Other
1k stars 215 forks source link

Race condition between rails-ujs and remotipart means "submit" buttons aren't disabled #119

Open danbernier opened 9 years ago

danbernier commented 9 years ago

Hi,

I think @treznick and I found a race condition between remotipart and jquery-ujs that is preventing buttons from being disabled ala data-disable-with, added in issue #31.

In development, we see UJS disable the buttons, then re-enable them on the ajax:aborted:file event, and then remotipart re-disables them for the ajax file upload.

But in production, UJS disables the buttons, then remotipart re-disables them, and then UJS enables them. It appears to the user that the buttons are never disabled.

Changing the remotipart setTimeout delay for re-disabling the buttons to 40ms, like so:

setTimeout(function(){ $.rails.disableFormElements(form); }, 40); // old timeout was 20

...seems to fix the race condition, but this seems like a brittle solution.

Our problem feels very similar to #338, but not quite the same.

Is there a better way to guarantee the order?

danbernier commented 9 years ago

Update: as of now, we have our own series of setTimeout callbacks at something like 10, 30, and 200ms, to re-disable the buttons, as long as there's a 'pending' flag on the form (which we're adding when the form is submitted, and removing when the response comes in). It's sloppy, but it works.

Would love to hear thoughts on fixing the core issue. Anything we can do to help?