Stimulus Discourse

Form ajax:success called for remote input change

I’m using rails-ujs (not jquery_ujs) and Stimulus 1.1.1.

I have a remote form, with a remote select input. Both the form and and the select input use the same stimulus controller. They both also use ajax:success. However, I thought the sucess event would be relevant to the input it was bound to.

By that I mean, I thought the forms ajax:success would only fire on successful form submission.

Likewise, I thought the select inputs ajax:success would only fire on change of the select.

What I am actually experiencing is that both ajax:success methods are called on select change.

Some sample code

# my_view.html.erb
<%= simple_form_for(@my_model,
  remote: true,
  html: {
    data: {
      controller: "dog",
      dog_url: dogs_path,
      action: "ajax:success->dog#stuffForFormSuccess"
    }
  }) do |f| %>

  <%= f.input :dog_id,
    collection: dog_query,
    input_html: {
      data: {
        remote: true,
        url: get_by_id_dogs_path,
        type: 'json',
        action: "ajax:success->dog#stuffForSelectChange",
        target: "dog.dogSelect",
        controller: "select2-dog"
      }
    }
  %>

<% end %>

So in the above example, on select input change, both stuffForFormSuccess and stuffForSelectChange are called even though the form wasn’t submitted.

Is this expected behaviour?

Does the select use AJAX to populate its fields in any way?

Thanks for the response. Yes, you are right. I use select2 which comes with built in AJAX support for loading data.