You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi,
Recently I have been trying to incorporate Turbo on a project that uses this gem and found a conflict regarding turbo and the toggle functionality.
Context
If we do not use the toggle property, everything runs perfectly, even with nested render_asyncs, either when the page is loaded for the first time or by navigating to that page.
When using the toggle property (without the start flag or with the start flag set to false), render_async is not executed when navigating to that page.
Example code
main page e.g.: app/views/general_pages/index.html.erb
<%= render_async example_path, toggle: { selector: '#example-button', start: false, event: :click } do %>
<button id='example-button'>Load example</button>
<% end %>
get "test", to: "general_pages#index"
get "example", to: "general_pages#example"
render_async initializer
RenderAsync.configure do |config|
config.turbo = true
config.jquery = true
end
On this example, if the initial page is /test everything runs as expected: We see the button and clicking on it renders the "Loaded example" text.
If our initial page is not /test, when we navigate to it we see the button but nothing happens when clicking on it.
Possible solution
After some digging I found that the _setUpToggle function on _request_jquery.js.erb is executed but the event listener function added to the $(document) object is not executed.
That event listener function that I am referring is this one: $(document).<%= toggle[:once] ? 'one' : 'on' %>('<%= toggle[:event] || 'click' %>', '<%= toggle[:selector] %>', function(event) {
I managed to make it work by changing the logic to the following:
For some reason, by adding the event listener directly on the element instead of the document fixed this issue and the function is triggered on the button click, even when navigating to it.
I just wanted to discuss this problem/solution and I'm more than happy to open a Pull Request if this solution makes sense. Thanks!
The text was updated successfully, but these errors were encountered:
pedroordep
changed the title
Turbo: Toggable event not triggering the _setUpToggle function on a new visit
Turbo: Toggable selector not triggering the _setUpToggle function attached to $(document) on a new visit
Apr 4, 2022
pedroordep
changed the title
Turbo: Toggable selector not triggering the _setUpToggle function attached to $(document) on a new visit
Turbo: Toggable selector not executing the event_listener function set by _setUpToggle on a new visit
Apr 4, 2022
Hi,
Recently I have been trying to incorporate Turbo on a project that uses this gem and found a conflict regarding turbo and the toggle functionality.
Context
If we do not use the toggle property, everything runs perfectly, even with nested render_asyncs, either when the page is loaded for the first time or by navigating to that page.
When using the toggle property (without the
start
flag or with thestart
flag set to false), render_async is not executed when navigating to that page.Example code
app/views/general_pages/index.html.erb
app/controllers/general_pages_controller.rb
app/views/general_pages/_example.html.erb
app/config/routes.rb
On this example, if the initial page is
/test
everything runs as expected: We see the button and clicking on it renders the "Loaded example" text.If our initial page is not
/test
, when we navigate to it we see the button but nothing happens when clicking on it.Possible solution
After some digging I found that the
_setUpToggle
function on_request_jquery.js.erb
is executed but the event listener function added to the$(document)
object is not executed.That event listener function that I am referring is this one:
$(document).<%= toggle[:once] ? 'one' : 'on' %>('<%= toggle[:event] || 'click' %>', '<%= toggle[:selector] %>', function(event) {
I managed to make it work by changing the logic to the following:
For some reason, by adding the event listener directly on the element instead of the document fixed this issue and the function is triggered on the button click, even when navigating to it.
I just wanted to discuss this problem/solution and I'm more than happy to open a Pull Request if this solution makes sense. Thanks!
The text was updated successfully, but these errors were encountered: