-
Notifications
You must be signed in to change notification settings - Fork 430
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
Events: Accessing the link or form element in dispatched events #99
Comments
One way that's currently possible involves listening for |
Well - not really. While one can listen to Besides that, as the |
What I miss the most is the the ability to customize the fetchOptions using hints given as data attributes. For example, a link could hint that it would accept a modal as content like so: <a href="/boosts/new" class="btn" data-rel="modal">Create boost</a> document.addEventListener("turbo:before-fetch-request", (event) => {
const { details: { originalEvent, fetchOptions } } = event;
if (originalEvent?.target && originalEvent.target.dataset.rel == "modal") {
fetchOptions.headers["Accept"] = ...
}
}); # app/views/boosts/new.html+modal.erb
<dialog>
...
</dialog> |
Hello, any updates regarding this? I badly need to access the form element on submission, for example for Google Recaptcha v3. To pause the submit request and wait for the token and then resume. But unfortunately the |
#367 I'm trying to listen for |
I just encountered the same issue. Having a reference to the original element that was interactively clicked would be super helpful. |
Would be happy to see a PR for originalEvent. Please do investigate. |
I don't have a deep understanding of this, but it appears that access to the export function dispatch(eventName: string, { target, cancelable, detail }: Partial<DispatchOptions> = {}) {
const event = new CustomEvent(eventName, {
cancelable,
bubbles: true,
detail,
})
if (target && (target as Element).isConnected) {
target.dispatchEvent(event)
} else {
document.documentElement.dispatchEvent(event)
}
return event
} See 3c726f7. A prerequisite being that the target was not removed from the DOM first. I just confirmed in our own Turbo app that clicking a link, while listening for the event via |
@dhh in a similar vein to #367, would it be preferable to dispatch const link = document.getElementbyId('link')
addEventListener('turbo:visit', (event) => event.target === link)
link.click() |
So long as it continues to bubble up to the |
@seanpdoyle #695 should do this. The default |
Most events fired by Turbo fire on
document
, irrespective of if the navigation was triggered programatically or through a link click or form submission. Theevent.details
for these events does not contain a reference to the original interactive event (click / form) nor a reference to the original element in case a navigation was fired interactively.I'd be very handy if these events (eg.
turbo:before-fetch-request
) would provide details with a reference to the link or form element that triggered the navigation. This would allow customisation based on the element's attributes, as well as updating the element during the lifecycle of the visit.A few common examples that are very hard to achieve today:
Add a
loading
class to the link or form or adding a spinner while the turbo request is in-flight (listening toturbo:before-fetch-request
)Customize the fetch options based the element attributes, eg.
<a href=".." data-prefer-modal> ..</a>
could customize theAccept
headers to allow the backend to return a specific variant.Handle network and server-side errors by adding an error class to the form in case the network fetch failed
Customize cache behavior based on the
action
A suggestion could be to provide
originalEvent
inevent.details
if a visit was triggered interactively, eg:It could be there are ways to do this today that I've missed. If not, what do you think? I'll be happy to wrap up a PR supporting this, as I believe it would be very useful for customizing the behavior when doing navigations.
The text was updated successfully, but these errors were encountered: