Laracasts Alpine code in an action
Im following a guide on laracasts and im at a point for it has a form component, and has attributes for: x-data, x-on:submit.prevent.
I am wondering if I can use a custom action to call the alpinejs function from my app.js file, rather than creating a new blade file etc
Bit lost trying to find this in the docs?
27 Replies
Anyone have any idea?
please provide mor info what you are trying to do..
So I have a function in my app.js called 'registerPasskeys' that has some js logic.
I want to have an action that fires off that js function
if you register your js file, you can call this using
->alpineClickHandler('registerPasskeys')
Ill give that a go when im back in the office tomorrow, thank you
Morning, I've just managed to have a test of this, im getting 'registerPasskey' is not defined, and im not sure the correct syntax.
The above is my alpinejs code.
I tried registerPasskey, registerPasskey.register and just register, none of which work
It looks like its searching the livewire.js file but I have it defined in my app.js file.
So moved code into a custom.js file,
Registered in service provider:
When I refresh browser, I initially get this:
And when I click my button, its still searching in the livewire.js file instead of my custom.js
Are you using
import axios
in your custom JS?Yeah
Custom.js
I see the "alpine initializing" console log, so the file is being read.
Just when I click the button I get this
Try to import via cdn instead of
Does that mean like this:
As thats spat out a whole lot of errors in dev tools,
If I move the logic outside of the Alpine.data function, it works. So its something to do with the Alpine.data not registering it as a component or its registered but the alpineClickHandler cant detect it.
sorry for the delay.. where are you using this action?
register
axios
and simplewebauthn
via cdn in your appserviceprovider
Try this
don't forget to run php artisan filament:assets
to publish the assetsSorry, I got caught in a meeting.
So the action is in a relationmanager, in the headerActions array.
I've added the code you linked above and got the following errors. The first 2 are just because Ill have to add the urls to the cors part of my docker container.
I got a cannot use import statement outside a module error, then that the registerPasskey is not defined.
This again being my code:
remove imports and register via cdn in your provider
Apologies, I'd finished work at this point yesterday,
So tried it this morning, removed the import lines from my js, added the cdn in my service provider, still to no avail
YESSS! Nevermind, it worked. The last part was the custom line in the provider, I had to change it back to Vite::asset(). And it now works.
2 days of stress, thank you so much. I can continue the rest of the course now 🤣
Would you mind explaining what those changes did exactly so I have an understanding of what went on
Filament supports importing from CDN. When using Alpine.data, it's necessary to specify the component object in x-data, as outlined in the docs.
Ah that makes sense, and so if when I call the register function, I want to pass a parameter, how would I do that?
I believe you can pass params.. check the alpine docs
Ah sorry, I mean from the alpineClickHandler point of view, is that just comma separated from the handler string?
Sorry if these questions are dumb founded, just new to all of this
Solution
probably something like this
->alpineClickHandler('register(xx)')
Got it thank you. Thank you for your patience with me 🙂
no problem!
Sorry for coming back to this one, so we left off with an action that works and looks like this...
Im wondering if its possible for an equivalent work to allow use of a Toggle component inside a form, as currently it says alpineClickHandler does not exist
maybe
$this->js('register(xx)')
in the afterStateUpdated?Thats now leaving me with
When I press the toggle, but the function in my js hasn't changed
I wasn't able to get anywhere with this, any ideas if im missing something else?
Thats my Alpine component, and this is my Toggle