Skip to content

Commit

Permalink
Do not execute hx-trigger="load" on re-initialization of an existing …
Browse files Browse the repository at this point in the history
…node (#2976)

* Do not execute hx-trigger="load" on re-initialization of an existing node

* simplify initNode firstInit logic
  • Loading branch information
basvk authored Dec 11, 2024
1 parent bd35f64 commit 34dda10
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/htmx.js
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,7 @@ var htmx = (function() {
* @property {XMLHttpRequest} [xhr]
* @property {(() => void)[]} [queuedRequests]
* @property {boolean} [abortable]
* @property {boolean} [firstInitCompleted]
*
* Event data
* @property {HtmxTriggerSpecification} [triggerSpec]
Expand Down Expand Up @@ -1625,7 +1626,7 @@ var htmx = (function() {
})
}
deInitOnHandlers(element)
forEach(Object.keys(internalData), function(key) { delete internalData[key] })
forEach(Object.keys(internalData), function(key) { if (key !== 'firstInitCompleted') delete internalData[key] })
}

/**
Expand Down Expand Up @@ -2632,7 +2633,7 @@ var htmx = (function() {
}, observerOptions)
observer.observe(asElement(elt))
addEventListener(asElement(elt), handler, nodeData, triggerSpec)
} else if (triggerSpec.trigger === 'load') {
} else if (!nodeData.firstInitCompleted && triggerSpec.trigger === 'load') {
if (!maybeFilterEvent(triggerSpec, elt, makeEvent('load', { elt }))) {
loadImmediately(asElement(elt), handler, nodeData, triggerSpec.delay)
}
Expand Down Expand Up @@ -2839,11 +2840,12 @@ var htmx = (function() {
return
}
const nodeData = getInternalData(elt)
if (nodeData.initHash !== attributeHash(elt)) {
const attrHash = attributeHash(elt)
if (nodeData.initHash !== attrHash) {
// clean up any previously processed info
deInitNode(elt)

nodeData.initHash = attributeHash(elt)
nodeData.initHash = attrHash

triggerEvent(elt, 'htmx:beforeProcessNode')

Expand All @@ -2868,6 +2870,7 @@ var htmx = (function() {
initButtonTracking(elt)
}

nodeData.firstInitCompleted = true
triggerEvent(elt, 'htmx:afterProcessNode')
}
}
Expand Down
11 changes: 11 additions & 0 deletions test/core/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,17 @@ describe('Core htmx API test', function() {
div.innerHTML.should.equal('delete')
})

it('does not trigger load on re-init of an existing element', function() {
this.server.respondWith('GET', '/test', 'test')
var div = make('<div hx-get="/test" hx-trigger="load" hx-swap="beforeend"></div>')
this.server.respond()
div.innerHTML.should.equal('test')
div.setAttribute('hx-swap', 'afterbegin')
htmx.process(div)
this.server.respond()
div.innerHTML.should.equal('test')
})

it('onLoad is called... onLoad', function() {
// also tests on/off
this.server.respondWith('GET', '/test', "<div id='d1' hx-get='/test'></div>")
Expand Down

0 comments on commit 34dda10

Please sign in to comment.