nathanvda / cocoon

Dynamic nested forms using jQuery made easy; works with formtastic, simple_form or default forms
http://github.com/nathanvda/cocoon
MIT License
3.08k stars 385 forks source link

Add nested form with JS call is impossible in a clean way #431

Open Antronin opened 7 years ago

Antronin commented 7 years ago

Hi!

I really like the gem, but currently it seems impossible to trigger the insertion of the nested form from JS. Of course I can trigger the click event on the button from JS but that will not give me the inserted fields list back, and it has anyway a bad code smell. Can you maybe merge https://github.com/nathanvda/cocoon/pull/290 pullrequest? I would give a muchcleaner JS API to cocoon.

nathanvda commented 7 years ago

The pullrequest you mentioned is dependant on jquery ui. I prefer not to introduce that dependency.

I understand that triggering insertion from javascript is at the moment not optimal. Could you explain your use-case?

Antronin commented 7 years ago

Hi! The use case: I have an image where I can select (via mous selection) multiple areas and I'm storing the coordinates in nested form fields. When I add a new fieldset via cocoon, I can create a selection on the image using the after-create callback. But I can do it in the opposite way: I make aselection on the image which should result in the creation of a nested form set with the coordinates of the selection in them. This later part is problematic, because there is no way of initiate the field set creation through JS. The only way I can think of is triggering the "click" event on the button, but that will trigger the after-create event too, which will duplicate my original selection.