jquery-events-to-dom-events
jquery-events-to-dom-events copied to clipboard
Suggestion: Include stimulus controller?
Hi! Handy library. Just wanted to suggest that perhaps it include a stimulus controller by default, and at least share what I did in case anyone else is interested.
import { Controller } from 'stimulus'
import { delegate, abnegate } from 'jquery-events-to-dom-events'
import $ from 'jquery'
window.$ = $
/**
* jQuery widgets like select2 and datetimepicker fire
* jQuery events when they change. Stimulus listens to native
* DOM events. This shim listens for a jQuery event and fires
* it as a native DOM event with $ prepended to the event name.
*
* Usage:
* data-controller="jquery-shim"
* data-jquery-shim-event-value="select2:select"
*
* Note: the default event is 'change', so if you want to
* listen to that event, the event-value attribute is optional.
*/
export default class extends Controller {
static values = {
event: { type: String, default: 'change' }
}
connect () {
this.delegate = delegate(this.eventValue)
}
disconnect () {
abnegate(this.eventValue, this.delegate)
}
}
Hope I got it right at least.
Hey, Tim! Including a sample controller is a good idea - but have I not already done so in the README?
Your controller is also somewhat implementation-specific. That is, many/most folks using this library probably don't need to pass in the event names. Generally if you're in the situation where you need this bridge, you know exactly what events you need to deal with. And you (often!) might need to handle more than one.
I do need to update the library dependency to @hotwired/stimulus.