Skip to content

Instantly share code, notes, and snippets.

@hopsoft
Created August 13, 2020 14:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hopsoft/13f10225c7cdc6d08a5150f192466a40 to your computer and use it in GitHub Desktop.
Save hopsoft/13f10225c7cdc6d08a5150f192466a40 to your computer and use it in GitHub Desktop.
StimulusReflex + SlimSelect that supports DOM mutations and reentrancy triggered by reflexes
<%= f.select :country_codes, country_select_data, {}, {multiple: true, data: {
controller: "select", action: "stimulus-reflex:after@document->select#delayedSetup", reflex: "change->Adapter#assign_attributes"}} %>
import ApplicationController from './application_controller'
import SlimSelect from 'slim-select'
export default class extends ApplicationController {
connect () {
this.setup()
}
disconnect (event) {
this.cleanup()
}
setup (event) {
this.cleanup()
this.slimSelect = new SlimSelect({
select: this.element
})
}
delayedSetup (event) {
setTimeout(this.setup.bind(this))
}
cleanup (event) {
if (!this.slimSelect) return
this.slimSelect.destroy()
delete this.slimSelect
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment