Skip to content

Commit c3a1d7a

Browse files
committed
add selectOOB and simplify ajax helper
1 parent 968dca9 commit c3a1d7a

3 files changed

Lines changed: 40 additions & 15 deletions

File tree

src/htmx.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4039,24 +4039,19 @@ var htmx = (function() {
40394039
returnPromise: true
40404040
})
40414041
} else {
4042-
let resolvedTarget = resolveTarget(context.target)
4042+
const { target, swap, source, event, ...restContext } = context
4043+
let resolvedTarget = resolveTarget(target)
40434044
// If target is supplied but can't resolve OR source is supplied but both target and source can't be resolved
40444045
// then use DUMMY_ELT to abort the request with htmx:targetError to avoid it replacing body by mistake
4045-
if ((context.target && !resolvedTarget) || (context.source && !resolvedTarget && !resolveTarget(context.source))) {
4046+
if ((target && !resolvedTarget) || (source && !resolvedTarget && !resolveTarget(source))) {
40464047
resolvedTarget = DUMMY_ELT
40474048
}
4048-
return issueAjaxRequest(verb, path, resolveTarget(context.source), context.event,
4049-
{
4050-
handler: context.handler,
4051-
headers: context.headers,
4052-
values: context.values,
4053-
targetOverride: resolvedTarget,
4054-
swapOverride: context.swap,
4055-
select: context.select,
4056-
returnPromise: true,
4057-
push: context.push,
4058-
replace: context.replace
4059-
})
4049+
return issueAjaxRequest(verb, path, resolveTarget(source), event, {
4050+
...restContext,
4051+
targetOverride: resolvedTarget,
4052+
swapOverride: swap,
4053+
returnPromise: true
4054+
})
40604055
}
40614056
} else {
40624057
return issueAjaxRequest(verb, path, null, null, {
@@ -4856,7 +4851,7 @@ var htmx = (function() {
48564851
selectOverride = xhr.getResponseHeader('HX-Reselect')
48574852
}
48584853

4859-
const selectOOB = getClosestAttributeValue(elt, 'hx-select-oob')
4854+
const selectOOB = etc.selectOOB || getClosestAttributeValue(elt, 'hx-select-oob')
48604855
const select = getClosestAttributeValue(elt, 'hx-select')
48614856

48624857
swap(target, serverResponse, swapSpec, {
@@ -5175,6 +5170,7 @@ var htmx = (function() {
51755170
* @property {string} [select]
51765171
* @property {string} [push]
51775172
* @property {string} [replace]
5173+
* @property {string} [selectOOB]
51785174
*/
51795175

51805176
/**
@@ -5223,6 +5219,7 @@ var htmx = (function() {
52235219
* @property {number} [timeout]
52245220
* @property {string} [push]
52255221
* @property {string} [replace]
5222+
* @property {string} [selectOOB]
52265223
*/
52275224

52285225
/**

test/core/api.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,16 @@ describe('Core htmx API test', function() {
319319
div.innerHTML.should.equal('<div id="d2">bar</div>')
320320
})
321321

322+
it('ajax api works with selectOOB', function() {
323+
this.server.respondWith('GET', '/test', "<div id='oob'>OOB Content</div><div>Main Content</div>")
324+
var target = make("<div id='target'></div>")
325+
var oobDiv = make("<div id='oob'></div>")
326+
htmx.ajax('GET', '/test', { target: '#target', selectOOB: '#oob:innerHTML' })
327+
this.server.respond()
328+
target.innerHTML.should.equal('<div>Main Content</div>')
329+
oobDiv.innerHTML.should.equal('OOB Content')
330+
})
331+
322332
it('ajax api works with Hx-Select overrides select', function() {
323333
this.server.respondWith('GET', '/test', [200, { 'HX-Reselect': '#d2' }, "<div id='d1'>foo</div><div id='d2'>bar</div>"])
324334
var div = make("<div id='target'></div>")
@@ -716,4 +726,20 @@ describe('Core htmx API test', function() {
716726
var path = sessionStorage.getItem('htmx-current-path-for-history')
717727
path.should.equal('/abc123')
718728
})
729+
730+
it('ajax api passes custom context properties to htmx events', function() {
731+
this.server.respondWith('GET', '/test', 'response')
732+
var div = make("<div id='d1'></div>")
733+
var customProp = null
734+
var handler = htmx.on('htmx:beforeRequest', function(event) {
735+
customProp = event.detail.etc.customProperty
736+
})
737+
htmx.ajax('GET', '/test', {
738+
target: '#d1',
739+
customProperty: 'testValue'
740+
})
741+
this.server.respond()
742+
customProp.should.equal('testValue')
743+
htmx.off('htmx:beforeRequest', handler)
744+
})
719745
})

www/content/api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ or
6565
* `values` - values to submit with the request
6666
* `headers` - headers to submit with the request
6767
* `select` - allows you to select the content you want swapped from a response
68+
* `selectOOB` - allows you to select content for out-of-band swaps from a response
6869
* `push` - can be `'true'` or a path to push a URL into browser location history
6970
* `replace` - can be `'true'` or a path to replace the URL in the browser location history
71+
* *custom properties* - any additional properties will be passed to some htmx events via `event.detail.etc`
7072

7173
##### Example
7274

0 commit comments

Comments
 (0)