-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Wrong detail.elt for htmx:beforeSwap #3000
Comments
Hey, indeed it's likely a bug, especially as the documentation says the opposite of the current behavior. |
From investigating why htmx is doing this i found this commit from 2020 6f14cba Trigger events on new content, rather than triggering element, since triggering element may have been replaced see #79 It seems that often the element has already been replaced at this stage so elt is no longer a valid and so the event has to fire on the target that is being swapped instead of the old possibly removed element. which can cause events to get lost it seems. So i think the documentation instead needs updating to make this 4 year old behavior clearer. I also think you can probably reliably find the requesting element in the additional detail provided in the event. try detail.requestConfig.elt |
That commit isn’t for the same lines, I think? (EDIT: nvm, that's a very old commit with many fewer lines, so it ends up being the right one) Besides, I don’t think this is intuitive behavior; changing the semantics of the same name ( |
I am also not sure why beforeSwap would be affected by this. In theory, none of the elements should have changed at that point, right? Or is this due to the event loop queueing events but getting to the swap before the events actually fire? I would think this latter scenario wouldn’t happen given that event was specifically made so that people can change the swap behavior. |
yeah re-looking at the commit I think it was in fact the afterSwap events that were broken and not the beforeSwap events. But it looks like that when he switched afterSwap to the destination element and not the requesting one to solve the issue the beforeSwap was also changed to the same to be symmetrical probably. So it probably didn't have to be changed originally in theory. But now that its been like this since almost the first version of htmx there will be many applications that now depend on their beforeSwap event working with the event listeners setup on the target elements. The problem is the detail.elt field is not just a data field but it is also used as the target for where the event is fired so changing its data also changes how people listen for the event :( Also while the documentation seems to have detail.elt as a very consistent description of
So as you can see the documentation is only about 50% right which is why the documentation probably needs updating. |
I’d hate to clutter up the event with another attribute. So, I think if this were to be implemented, I think it would make sense to make it more comprehensive and have more purpose than fixing this one issue. Given that, having an attribute on all events that stores a deep-ish copy of the element that made the request (so that it doesn’t disappear because it got swapped) would be good, I think. Is The reason why is that I could see there being cases (my extension is one of them) in which the users of the events for which the element has been deleted might still need attributes on that element to properly do what they want to do. After all, that element does contain almost all the meta information about the htmx action taken, even things that were written for an extension, which, therefore, htmx (I would think) doesn’t track. In theory, it would make the most sense to save in that variable the element that also references the state of the DOM at that time so that all the information is available, but I don’t know how realistic that is from a memory usage perspective; maybe it actually isn’t so bad if, say, it’s a kind of diff between the old and the new. |
Hi, I am trying to write an extension using the
htmx:beforeSwap
event, and it appears that line 4714:if (!triggerEvent(target, 'htmx:beforeSwap', beforeSwapDetails)) return
ends up resulting in
detail.elt
being set totarget
(astarget
is passed into the first parameter oftriggerEvent
, which setsbeforeSwapDetails.elt
to it). However, other calls totriggerEvent
pass inelt
instead. Also, in the documentation at https://htmx.org/events/#htmx:beforeSwap, it says thatdetail.elt
is supposed to be "the element that dispatched the request". This makes more sense to me because it makes beforeSwap more useful (you get to additionally access the request element), but also becausedetail.target
already exists and can be used.Is this intended behavior? As stated earlier, it can be really difficult to take action on this event if you don't know which element sent the request.
The text was updated successfully, but these errors were encountered: