YouTubevideo· Aug 9, 2024· 12:00

Is this better than HTMX?

Watch on YouTube ↗

transcript

29 moments
  1. I'm not going to lie I think I finally understand Progressive enhancement there's a reason why tools like hmx and in this case what I'm showing is a deeper dive into Alpine Ajax which I found a little bit ago but I've been really loving is because you can build a project a product using just lelt using

  2. just blade and then add some fancy Ajax stuff to it now I'm not giving up Live Wire I love live wire I love what you can do and I love having everything in one code file which is why I love live wire volt but I get for some projects this is great especially if it's a very serers side rendered and server driven project and there's stuff that you can

  3. do where you can just have a to-do list like this but enhance it so it feels quick it feels fast but without changing anything without adding too much JavaScript and without breaking it when JavaScript is disabled so this is just straight blade right now we have a new to-do uh that can add here we click submit it refreshes the page which pulls

  4. in the new to-dos how can we add Alpine Ajax and which is Alpine's version of HTM X so Alpine Ajax uh is basically an HDMX alternative I should say but with a little bit more Alpine Centric method so we have an X init X Target we have things like this dollar sign Ajax where we can call Ajax methods within our

  5. Alpine within our HTML Alpine Ajax is just an alternative to HTM X but there's some neat little things that I've learned about it and when I made a previous video just a couple of days ago talking about Alpine Ajax as a plugin I initially thought it was very similar to HTM X in the sense that if I'm writing some code and I have this web route if I

  6. want an HTM X version I need to just return the HTML that I want to render the cool thing is we're not going to change anything in this route and Alpine Ajax just works in the sense where we're just returning the new route returning back in this case refreshing the page so that we can pass in those to-dos to the welcome View and here's what's going to

  7. happen I have Alpine Ajax already uh registered and installed and if you do this you follow the instructions I use lvo Breeze which has Live Wire already installed but I need to register an Alpine plugin like this installing Alpine from this live wire. esm and this is just because I used Breeze to have Alpine JS and tailwind and all that installed from the get-go but if you're using larel blade you can install Alpine

  8. in that way if you're using the blade version of larel Breeze I should say say now in our welcome view all I have to do is add to this form we have an action already and this is some data to just say we're going to set the text to that data so we can reset it on form load what we just need to do is say x andit and then X Target equals and then we have this idea of to-dos down here so we

  9. want to Target the to-dos and then that's it surprising right let's give it a go refreshing the page added to do hello there submit and it just works let's see let's inspect the page and take a look at the network requests hi YouTube submit pop

  10. this down here we have this network request for too which is essentially returning the same page as this to-do and you can see here it's returned the same page because we now have to clear this right here it's not a refresh this isn't being cleared before if I was to remove these two things again X andit and x- Target save this

  11. refresh clear submit it's cleared because it's it's a full refresh if I want to let's go ahead and show in the web maybe we'll say We'll sleep this for a little bit here sleep of 2 seconds this is the full page refresh hello there submit full page refresh you can see that going on

  12. up there but if I was to add that X and knit and X Target back in add a new one submit you can see that the page is not reloading it's just waiting for it to get back that Ajax using Alpine Ajax why don't I go ahead and clear these out real quick just to

  13. start off fresh okay how can we do that with this delete button down here again this is just larel bra blade we have a method of post and we don't necessarily even need to call this method there we just have the csrf with a method of delete here so I can do the same thing we can add X that Dash knit and then X Target of to-dos because all

  14. we're asking for in our web route is we're returning back the page but Alpine Ajax says what is the to-do Target and I'm going to replace those so new one submit we're sleeping for a new one no sleep and now let's delete one and get method is not supported aha

  15. because we do need the method of post I don't know why I delete that method poost okay because lar a blade does need that we'll refresh delete and it just works now I wonder what if we remove this x Target I believe I think it just refreshes the page let's

  16. see let's change this sleep here move this target to just to see yeah that does look like a full page refresh so I think the target shows you that or shows Alpine Ajax that when this page does change we're going to look for the IDE of to-dos and then refresh that

  17. so we'll do that same thing here x Target equals todos and delete delete new one howdy there we go okay Alpine Ajax I I get it there's so much more that I haven't necessarily been able to dive into just yet in terms of okay what is happening like when it's loading

  18. we'll go ahead and see like loading indicator here we have this x Target and then we can add Alpine Ajax adds Arya Das busy equals true to targets while a request is processing okay very interesting so I'm curious if I just throw this in here let's see what this does I throw this into my app.css

  19. and refresh hi there oh interesting that's neat see okay so there's probably a bunch of different ways to do that but that's the way that they're just showing in their example here I like it it's similar to Live Wire we have this wire loading so you have this Arya Das busy attribute that's being added to this element let's see

  20. what else so you can have multiple targets so it's the form is the contacts form okay so very interesting I think that's just HTML though the ajax form is targeting the contact table very interesting I bet it sends that along yeah the part that really just uh surprised me was that I wouldn't have to add anything to the route usually I would have thought I how would have had to return a fragment that is then shown

  21. in place of whatever my target is but Alpine Ajax is it's fancy in the sense that it's looking for whatever todos is then changed in that case our Target of to-dos we're just changing that out and we don't have to add anything else again we only added xit X Target Tod do and everything works and the part that we'll need to change here is if we take a look

  22. at up here hi there this isn't changed this does not refresh because there's is no page load the web rout is returning refreshed and we can disable JavaScript and it would still all work the same way but what needs to happen then is we need to figure out how to clear this text and this is why I modeled it here when we can go like this we can say

  23. Ajax and we can say on success I believe that's what they do let's take a look reference Ajax success yeah Ajax okay yeah success yeah so we can say equals and then we can say text that should be it because we're saying on we want the text in case the

  24. modeling the text that should be it right let's see there we go I'm pressing enter instead of pressing this AIT butun because it's a form so new one and because we're clearing the text on the Ajax request success and I wonder if we can even say okay only when this let's say I have multiple because if I click this click

  25. delete I would assume that this might clear it doesn't clear very interesting so it must do something where this Ajax success is only for this form Ah that's very nice so you can say

  26. I only want this to be cleared in this case if this forms Ajax request is successful I wonder if there's a way to do it for any Ajax requests let's see Ajax no okay either way doesn't really matter too much but that being said I understand why something like this is

  27. popular the appeal for Alpine Ajax for HDMX for Stuff of that uh regard because while I'm not going to be changing from Live Wire anytime soon if I had a specific use case where I needed this to be progressively enhanced AKA just a strict blade server rendered forms for all of my projects but it'd be nice to have Ajax every once in a while I might reach for something like this where I don't necessarily have

  28. to worry about what happens if someone's on a slow internet and JavaScript isn't loading quite yet or maybe it's for healthc care and they don't really have JavaScript enabled for whatever reason on their devices so I get it I think I'm a Believer now so yeah whatever you're building whatever you're Conjuring up within your projects maybe Progressive enhancement can work for you and if so

  29. take a look at Alpine Ajax in anyways keep creating

just you here