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
Rails-ujs events not firing from links loaded through rails-ujs #32438
Comments
Could you please provide the source code of your example? |
It seems that the listener for diff --git a/app/views/examples/_number_with_link.html.erb b/app/views/examples/_number_with_link.html.erb
index 4fe0f54..3c01b39 100644
--- a/app/views/examples/_number_with_link.html.erb
+++ b/app/views/examples/_number_with_link.html.erb
@@ -1,6 +1,6 @@
<p>
You clicked <%= @number %>. <br />
- Now click this link loaded through ajax: <%= link_to "Link ##{@number}", example_path(@number), remote: true %>
+ Now click this link loaded through ajax: <%= link_to "Link ##{@number}", example_path(@number), remote: true, id: 'link' %>
</p>
<p>
loaded at: <%= Time.now %>
diff --git a/app/views/examples/show.js.erb b/app/views/examples/show.js.erb
index 646cda1..e8f8715 100644
--- a/app/views/examples/show.js.erb
+++ b/app/views/examples/show.js.erb
@@ -1 +1,7 @@
document.getElementById('show').innerHTML = "<%= escape_javascript(render partial: 'number_with_link') %>"
+
+var link = document.getElementById('link');
+link.addEventListener('ajax:complete', function (xhr, status) {
+ console.log('Native ajax:complete')
+ document.getElementById('timer').innerHTML = `<p>Native ajax:complete at ${new Date}</p>`
+}) |
Thanks for looking in to this. Your patch works and is similar to a workaround I'm using now. I've implemented it here https://shielded-beach-23078.herokuapp.com/ with some basic annotation as to where the event came from. I can see from the source that
My understanding from this is the events from the ujs loaded content are getting fired , they are just not bubbling to document up like the events from the initial content. Is that correct? |
I think that is correct in my understanding. Perhaps there may be browsers that it works, but its behavior is not guaranteed. |
Anyway, this does not seem Rails' issue. I close this. Thanks. |
I'm thinking it's a race condition of sorts. The element that fired the request is being replaced by the incoming content before the |
Steps to reproduce
I've created a quick example app here: https://shielded-beach-23078.herokuapp.com/examples
The source is here: https://github.com/hughkelsey/rails-ujs-issue
Expected behavior
The ajax:complete event should fire whenever the
data-remote: true
link is clicked.Actual behavior
The ajax:complete event fires for the links initially loaded but not for the links loaded through rails-ujs.
System configuration
Rails version: 5.2rc2
Ruby version: 2.4.1
This issue was originally noticed in 5.1 but the test case above is in 5.2rc2
The text was updated successfully, but these errors were encountered: