Coder Social home page Coder Social logo

preventDefault issue about cocoon HOT 4 OPEN

hksk avatar hksk commented on August 19, 2024
preventDefault issue

from cocoon.

Comments (4)

nathanvda avatar nathanvda commented on August 19, 2024

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).

from cocoon.

hksk avatar hksk commented on August 19, 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.

from cocoon.

nathanvda avatar nathanvda commented on August 19, 2024

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.

from cocoon.

hksk avatar hksk commented on August 19, 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.

from cocoon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.