-
Notifications
You must be signed in to change notification settings - Fork 483
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
Add events to use:inertia
directive
#237
Conversation
8d88e82
to
b3484ce
Compare
Thanks for trying to make this work @pedroborges. 👍
So, I wonder, can you not make them callbacks (plain function props), and not listeners? What pushed you to make them listeners? Simply because they started with "on"? |
Also, one more quick question...does this actually run the event listener (callback)? From what I can tell from this code, it doesn't...it just fires the event. Which, at that point, you'd be better off just using the new event system directly. |
They could be function props on the
I have implemented it this way because from what @claudiodekker told me you can use
Internally, the options.onCancelToken = cancelToken => fireEvent('cancelToken', { detail: { cancelToken } })
options.onStart = visit => fireEvent('start', { cancelable: true, detail: { visit } })
options.onProgress = progress => fireEvent('progress', { detail: { progress } })
options.onFinish = () => fireEvent('finish')
options.onCancel = () => fireEvent('cancel')
options.onSuccess = page => fireEvent('success', { detail: { page } })
function fireEvent(name, eventOptions) {
return node.dispatchEvent(new CustomEvent(name, eventOptions))
} These callbacks are then passed as Inertia.visit(href, options) When
This is different because it doesn't fire a global event, only listeners attached to the active DOM node will be run. I have done a bit more of research and couldn't find Svelte UI kits that use function props for handling component events. The ones I found all use this methods. |
Alright, so, just to make sure I understand you correctly, this is my assumption of what's happening. First, the Then, the Inertia.visit is called, with amongst others the Then, when the
Because the 2nd step, when the local callbacks get fired, the This user-defined 'listener' method (step 1) then gets executed, returns it's value to the |
That's all correct except for the last paragraph:
In my tests I wasn't able to forward the return value from user-defined listener back to Inertia. I read something on the Inertia Discord last week that this might be specific to Firefox, my default browser. Need more testing around this part, everything else works just fine. |
use:inertia
directiveuse:inertia
directive
Closing this in favor of #1344. |
I was able to replicate #235 on the
use:inertia
directive like so:Also works on the
InertiaLink
component:The main difference is that these are not simple callbacks, but event listeners. In Svelte you can't access event listeners like props during runtime because they are processed by Svelte during compilation. The solution is to register
on*
callbacks that fire custom events on the DOM node.The only issue so far is that I couldn't find a way to either return
false
or prevent theon:start
event and forward that response back to theonStart
callback internally.This PR also removes logic that was duplicated on the
use:inertia
directive andInertiaLink
component. Now the component uses the directive internally. Double win!