I want to call a modal on a view with ajax: success
event, use stimulus
view:
div [
data-controller= "modal-loader"
data-action="ajax:success->modal-loader#loadModal"
]
= render Buttons::SaveButton.new
= render Modals::Modal.new(modal_title: 'Modal')
controller:
def create
@voc_city = VocCity.new(params[:voc_city])
authorize @voc_city.becomes(VocCity)
if @voc_city.save
head :ok
else
respond_with @voc_city, status: :unprocessable_entity
end
end
But the modal won’t get called, what am I doing wrong?
Be sure to specify for the form remote: true
n4cr
March 4, 2021, 9:39pm
3
Is ajax;success
a jQuery event? If so, you will need to delegate it to a normal event. You can check
Motivation
Some of you might have worked on projects using jQuery and struggled with the lacking ability to listen for jQuery events on Stimulus actions.
I was working on a project where I needed to listen to the jquery_ujs request lifecycle events like ajax:success on my Stimulus actions and I didn’t have the option to replace it with rails-ujs, so I wrote a small wrapper in CoffeeScript to solve this issue.
Solution
How it works
There’s not a lot to it. Internally it catches, converts…