Skip to content

Commit 5596be9

Browse files
authored
Add the ability to specify '_none' as a target (#107)
1 parent c7dfe6b commit 5596be9

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,11 @@ async function send(control, action = '', method = 'GET', body = null, enctype =
340340

341341
let focused = !plan.focus
342342
let renders = targets.map(async target => {
343+
344+
if (target._ajax_id === '_none') {
345+
return
346+
}
347+
343348
if (target === document.documentElement) {
344349
window.location.href = response.url
345350
}
@@ -420,7 +425,7 @@ function createTargets(plan, controller) {
420425

421426
let targets = plan.ids.map(pair => {
422427
let docId = pair[0]
423-
let el = ['_self', '_top'].includes(docId) ? document.documentElement : document.getElementById(docId)
428+
let el = ['_self', '_top', '_none'].includes(docId) ? document.documentElement : document.getElementById(docId)
424429
if (!el) {
425430
console.warn(`Target [#${docId}] was not found in current document.`)
426431
return

tests/status.cy.js

+36
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,39 @@ test('targeting `_top` will reload the page when redirected back to the same URL
210210
})
211211
}
212212
)
213+
214+
test('targeting `_none` will neither reload the page nor attempt to alter the DOM',
215+
html`<form x-target x-target.302="_none" id="form"
216+
@ajax:success="document.getElementById('form').id = 'success'"
217+
@ajax:missing="document.getElementById('success').id = 'missing'"
218+
@ajax:merge="document.getElementById('success').id = 'merge'"><button id="button"></button></form>`,
219+
({ intercept, get, wait }) => {
220+
intercept('POST', '/tests', (request) => {
221+
request.redirect('/tests', 302)
222+
})
223+
intercept('GET', '/tests', {
224+
statusCode: 200,
225+
body: ''
226+
}).as('response')
227+
get('button').click()
228+
wait('@response').then(() => {
229+
get('#button').should('exist')
230+
get('#success').should('exist')
231+
})
232+
}
233+
)
234+
235+
test('targeting `_none` for redirects will not prevent non-redirect responses from updating the DOM',
236+
html`<form x-target x-target.302="_none" id="replace"><button id="button"></button></form>`,
237+
({ intercept, get, wait }) => {
238+
intercept('GET', '/tests', {
239+
statusCode: 200,
240+
body: '<div id="replace">Replaced</div>'
241+
}).as('response')
242+
get('button').click()
243+
wait('@response').then(() => {
244+
get('#button').should('not.exist')
245+
get('#replace').should('have.text', 'Replaced')
246+
})
247+
}
248+
)

0 commit comments

Comments
 (0)