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

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

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1628,7 +1629,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 @@ -2634,7 +2635,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 @@ -2841,11 +2842,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 @@ -2870,6 +2872,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 @@ -383,6 +383,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