nested_form icon indicating copy to clipboard operation
nested_form copied to clipboard

addFields javascript being executed multiple times

Open joraff opened this issue 11 years ago • 4 comments

I'm noticing something strange going on with nested_form and the click event on the add link where multiple empty rows are inserted from the blueprint with a single add link click. I highly doubt this involves nested_forms at all, but I'm only seeing this behavior here.

  1. Clear browser cache
  2. Navigate to your form that uses nested_forms
  3. Click add link -> one row is inserted from the blueprint
  4. Navigate to the same url as in step 2, and not by refreshing the browser
  5. Click add link -> two rows are inserted from the blueprint
  6. Repeat 4-5 n times and n + 1 rows are inserted with one click

This continues until the page is refreshed.

The blueprint content is correct. Placing a breakpoint in addFields shows, from the call stack, that jQuery is processing the click event multiple times.

Anyone else seen this?

joraff avatar Jan 16 '14 19:01 joraff

Problem was with turbolinks. Removed from pipeline, works fine now.

joraff avatar Jan 16 '14 20:01 joraff

I had the same issue, when removing turbolinks it was resolved. Thanks!

naturegirl avatar Apr 11 '14 00:04 naturegirl

Has anyone fixed this? I am still having the issue even after I removed Turbolinks. It adds two entries every time I click Add...

JSanderbeck avatar Mar 19 '16 22:03 JSanderbeck

Ok. I figured out how to fix it. You first have to remove the event. Also converted to .on as that is the preferred method now.

window.nestedFormEvents = new NestedFormEvents(); $(document) .off('click','form a.add_nested_fields').on('click','form a.add_nested_fields', nestedFormEvents.addFields) .off('click','form a.remove_nested_fields').on('click','form a.remove_nested_fields', nestedFormEvents.removeFields); })(jQuery);

JSanderbeck avatar Mar 20 '16 14:03 JSanderbeck