Rails, Turbolinks, Stimulus and asset pipeline
See original GitHub issueWhat’s the best way of installing Stimulus with Rails, Turbolinks using the asset pipeline?
I’m having an issue with the Stimulus action function firing multiple times after I visit another page and then come back to the original page with the Stimulus controller.
I have installed Stimulus by just downloading the https://unpkg.com/stimulus/dist/stimulus.umd.js file and then pulling it into application.coffee
using #= require stimulus.umd
I was thinking I could call @application = Stimulus.Application.start()
inside application.coffee
file but then it is run on every page visit.
Should @application = Stimulus.Application.start()
and @application.register "filter", class extends Stimulus.Controller
be called from within a Turbolinks event? I tried a few different events but each time the action function would be called multiple times after revisiting the page.
Here is my current setup:
application.coffee
#= require jquery
#= require jquery_ujs
#= require turbolinks
#= require stimulus.umd
#= require_self
#= require filter
@application = Stimulus.Application.start()
filter.coffee
@application.register "filter", class extends Stimulus.Controller
update: ->
# Code should only be called once each time the action triggered.
# After visiting another Turbolinks page and coming back this is called multiple times each time the action is triggered.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:7
- Comments:6 (3 by maintainers)
Top GitHub Comments
Closing this for now, but feel free to continue the conversation over on the new Stimulus Discourse.
@javan I created an example app and it seems to be working as expected so there is probably something else in my main app that’s causing the events to fire twice.
One thing I did notice is that the Stimulus controller is initialized twice when I visit the page using a Turbolinks link. I don’t think it’s causing an issue but just something that was interesting, here’s a gif showing that.
If I just refresh the page with the Stimulus controller then the controller is only initialized once.
Here’s a link to my example app https://github.com/scottharvey/stimulus_asset_pipeline