Lukas W.
Well-known member
- Affected version
- 2.3 Beta 1
It looks like all events triggered with
In my specific instance I have an auto-complete field inside a modal, that I have attached a listener to with the code below. Both the target, and the JS are loaded through the modal. Since the target is not yet present when the XF loader loads the JavaScript in, the event listener can't be immediately attached to the event, so I've chosen the document body as target instead.
When the
This appears to be a general problem with how XF triggers events throughout the code. jQuery events always bubble upwards unless specifically configured not to, so it's probably preferable to change
XF.customEvent
are configured to not bubble up through the DOM. The constructor specifically deletes the bubbles
option from the config. As such, it is impossible to attach any listener to such an event if the event target is not immediately present when the JS is loaded, such as when the target is inside a modal.In my specific instance I have an auto-complete field inside a modal, that I have attached a listener to with the code below. Both the target, and the JS are loaded through the modal. Since the target is not yet present when the XF loader loads the JavaScript in, the event listener can't be immediately attached to the event, so I've chosen the document body as target instead.
JavaScript:
XF.on(document.body, 'change pase auto-complete:insert', function (event) {
const target = event.target.closest('<actualTargetSelector>');
if (!target) {
return;
}
// Do stuff
});
When the
auto-complete:insert
event is fired however, it is triggered through XF.trigger(this.target, XF.customEvent('auto-complete:insert'))
, and as such runs without bubbles: true
, even if it was defined on the specific trigger section (which it is not).This appears to be a general problem with how XF triggers events throughout the code. jQuery events always bubble upwards unless specifically configured not to, so it's probably preferable to change
XF.trigger
to always bubble events up (unless configured otherwise).