helpers: remove element immediately when no timeout is specified

This fixes an issue where clicking quickly on several "Remove row"
buttons on a repetition field results in autosave errors.

This is because the N+1 autosave request is correctly sent after the
Nth response from the server, but _before_ the row element is actually
removed from the DOM. So the N+1 request actually sends the fields for
the deleted row, which makes the server raise an error.

With this fix, the row gets properly removed when the server responds,
and before the next request is started.
This commit is contained in:
Pierre de La Morinerie 2020-08-25 12:47:13 +02:00
parent ecc4f01c20
commit 3fdecf0924

View file

@ -62,7 +62,11 @@ module ApplicationHelper
script = "(function() {";
script << "var el = document.querySelector('#{selector}');"
method = (inner ? "el.innerHTML = ''" : "el.parentNode.removeChild(el)")
script << "if (el) { setTimeout(function() { #{method}; }, #{timeout}); }";
if timeout.present? && timeout > 0
script << "if (el) { setTimeout(function() { #{method}; }, #{timeout}); }"
else
script << "if (el) { #{method} };"
end
script << "})();"
# rubocop:disable Rails/OutputSafety
raw(script);