Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preventDefault issue #632

Open
hksk opened this issue May 6, 2024 · 4 comments
Open

preventDefault issue #632

hksk opened this issue May 6, 2024 · 4 comments

Comments

@hksk
Copy link

hksk commented May 6, 2024

Im using adminsitrate with nestedHasmany gem, and I got some errores for cocoon + turbo

currently I report a issue here:
nickcharlton/administrate-field-nested_has_many#81

when a Turbo page is loaded , the event is added to "Add" button , so If I navigate 10 times with turbo, when i click the add button, then 10 childs forms are created

I do a bad patch to fix that

replacing the add field event to remove the listener

  document.querySelectorAll('.add_fields').forEach(function(element) {
    // Correctly remove the event listener
    element.removeEventListener('click', addFieldListener);
    // Add the event listener again
    element.addEventListener('click', addFieldListener);
  });

  function addFieldListener(e) { ...

and works.. someone knows if this is a normal issue?

@nathanvda
Copy link
Owner

Which version of cocoon/rails/turbo are you using? In the current cocoon code (which is already pretty old) there is no addFieldListener but we dynamically catch click-events on the link.

Try installing the latest version of cocoon (1.2.15).

@hksk
Copy link
Author

hksk commented May 6, 2024

I add a addFieldListener to make it works, with the original code, not working
gem

  • cocoon 1.2.15
  • turbo-rails (2.0.5)
  • rails Rails 7.1.3.2
    js
  • "@hotwired/turbo-rails": "^7.3.0",

to make it better I paste the diff:

40c40,47
<   $(document).on('click', '.add_fields', function(e) {
---
>   document.querySelectorAll('.add_fields').forEach(function(element) {
>     // Correctly remove the event listener
>     element.removeEventListener('click', addFieldListener);
>     // Add the event listener again
>     element.addEventListener('click', addFieldListener);
>   });
> 
>   function addFieldListener(e) {
103c110
<   });
---
>   };
134c141,142
<   var hideRemoveFields = function() {
---
> 
>   $(document).on("ready page:load turbolinks:load", function() {
141c149
<   };
---
>   });
143,147d150
<   $(function() {
<     hideRemoveFields();
<     $(document).on('page:load turbolinks:load turbo:load', hideRemoveFields); // Turbolinks support
<   });
<   
150a154
> 

after the change, the duplications stops and works fine.

@nathanvda
Copy link
Owner

Where are you including your javascript in the page? It seems with each navigation/load the javascript is evaluated again, with turbo this should normally not be the case.

@hksk
Copy link
Author

hksk commented May 7, 2024

Im using administrate gem, they include in the top part of the layout:

https://github.com/thoughtbot/administrate/blob/main/app/views/layouts/administrate/application.html.erb#L27

but after verify my installed gem of administrate I see it in the bottom part

I will update to the master branch of the gem or the beta and back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants