Skip to content

Instantly share code, notes, and snippets.

@philsmy
Last active December 27, 2019 05:48
Show Gist options
  • Save philsmy/ea7037981ffce665e036c5ad7cca7d64 to your computer and use it in GitHub Desktop.
Save philsmy/ea7037981ffce665e036c5ad7cca7d64 to your computer and use it in GitHub Desktop.
Select2 and Stimulus with events
import { Controller } from "stimulus"
import $ from 'jquery';
export default class extends Controller {
connect() {
console.log('connecting Filter controller');
$("#subscriber-tags-select").on('select2:select', function () {
console.log("list item selected");
let event = new Event('change', { bubbles: true }) // fire a native event
this.dispatchEvent(event);
});
}
updateToTagOptions() {
console.log("updateToTagOptions");
$.ajax({
type: "GET",
url: "/test_json/filter_results.json",
success: (data) => {
console.log('Test json received!')
}
})
}
}
<%= form_with(model: @email_campaign, local: true) do |form| %>
<div class='card mt-4'>
<div class='card-header'>
Tags
</div>
<div class='card-body' data-controller='filter select2'>
<%= form.select :tags, SubscriberTag.all.map{ |t| [t.name,t.id]}, {include_blank: true, required: false, include_hidden: false}, class: 'form-control subscriber-tags', multiple: 'multiple', id: 'subscriber-tags-select', 'data-action':'change->filter#updateToTagOptions' %>
</div>
</div>
<%= form.submit %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment