Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to add options argument to event listener #2836

Merged
merged 12 commits into from
Aug 29, 2024
Merged
24 changes: 16 additions & 8 deletions src/htmx.js
Original file line number Diff line number Diff line change
Expand Up @@ -1236,26 +1236,33 @@ var htmx = (function() {
* @property {EventTarget} target
* @property {AnyEventName} event
* @property {EventListener} listener
* @property {Object|boolean} options
*/

/**
* @param {EventTarget|AnyEventName} arg1
* @param {AnyEventName|EventListener} arg2
* @param {EventListener} [arg3]
* @param {EventListener|Object|boolean} [arg3]
* @param {Object|boolean} [arg4]
* @returns {EventArgs}
*/
function processEventArgs(arg1, arg2, arg3) {
function processEventArgs(arg1, arg2, arg3, arg4) {
if (arg4 == null) {
arg4 = {}
bencroker marked this conversation as resolved.
Show resolved Hide resolved
}
if (isFunction(arg2)) {
return {
target: getDocument().body,
event: asString(arg1),
listener: arg2
listener: arg2,
options: arg3
}
} else {
return {
target: resolveTarget(arg1),
event: asString(arg2),
listener: arg3
listener: arg3,
options: arg4
}
}
}
Expand All @@ -1267,13 +1274,14 @@ var htmx = (function() {
*
* @param {EventTarget|string} arg1 the element to add the listener to | the event name to add the listener for
* @param {string|EventListener} arg2 the event name to add the listener for | the listener to add
* @param {EventListener} [arg3] the listener to add
* @param {EventListener|Object|boolean} [arg3] the listener to add | options to add
* @param {Object|boolean} [arg4] options to add
* @returns {EventListener}
*/
function addEventListenerImpl(arg1, arg2, arg3) {
function addEventListenerImpl(arg1, arg2, arg3, arg4) {
ready(function() {
const eventArgs = processEventArgs(arg1, arg2, arg3)
eventArgs.target.addEventListener(eventArgs.event, eventArgs.listener)
const eventArgs = processEventArgs(arg1, arg2, arg3, arg4)
eventArgs.target.addEventListener(eventArgs.event, eventArgs.listener, eventArgs.options)
})
const b = isFunction(arg2)
return b ? arg2 : arg3
Expand Down
15 changes: 15 additions & 0 deletions test/core/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ describe('Core htmx Events', function() {
}
})

it('events accept an options argument', function() {
bencroker marked this conversation as resolved.
Show resolved Hide resolved
var invoked = 0
var handler = htmx.on('custom', function() {
invoked = invoked + 1
}, { once: true })
try {
var div = make("<div hx-post='/test'></div>")
htmx.trigger(div, 'custom')
htmx.trigger(div, 'custom')
invoked.should.equal(1)
} finally {
htmx.off('custom', handler)
}
})

it('htmx:configRequest allows attribute removal', function() {
var param = 'foo'
var handler = htmx.on('htmx:configRequest', function(evt) {
Expand Down
5 changes: 5 additions & 0 deletions www/content/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,14 @@ Adds an event listener to an element

* `eventName` - the event name to add the listener for
* `listener` - the listener to add
* `options` - an [options](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#options) object (or a [useCapture](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#usecapture) boolean) to add to the event listener (optional)

or

* `target` - the element to add the listener to
* `eventName` - the event name to add the listener for
* `listener` - the listener to add
* `options` - an [options](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#options) object (or a [useCapture](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#usecapture) boolean) to add to the event listener (optional)

##### Example

Expand All @@ -331,6 +333,9 @@ or

// add a click listener to the given div
var myEventListener = htmx.on("#my-div", "click", function(evt){ console.log(evt); });

// add a click listener to the given div that should only be invoked once
var myEventListener = htmx.on("#my-div", "click", function(evt){ console.log(evt); }, { once: true });
```

### Method - `htmx.onLoad()` {#onLoad}
Expand Down