Skip to content
Advertisement

Stimulus.js call method defined in connect() lifecycle callback

Im still learning stimulus.js and I’m trying to expand on the DriftingRuby episode on stimulusJS and FullCalendar. In that tutorial the form submits a normal http put request and the page is reloaded. I’d like to allow users to manage events using UJS/Stimulus and not requiring a page reload.

This is my calendar_controller.js

JavaScript

I need to call the add_event method inside connect() lifecycle callback. As I’m learning Stimulus.js Im having a hard time finding examples where someone is trying to do something similar.

Is it possible to call the add_event method from outside the connect() method?

Advertisement

Answer

Move the function outside of the connect() function and you can call it from any function (with a few caveats).

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement