YouTubevideo· Apr 8, 2024· 12:12
have you ever had that feeling as a developer when you see a really cool demo or some neat project and you're like I have to understand exactly how that's working the Way It Is Well I was recently plagued with that feeling when I watched this video by Caleb porio doing optimistic UI with LL Livewire basically before the actual server Returns the response within larvo Live Wire show the user what you're expecting an optimistic output well he didn't
exactly show how to do it so I fig figured it out for myself [Music] bro now I don't know if this is exactly 100% how Caleb was doing it in this particular demo but after I saw this demo I kind of replayed through my mind of how you could do something like this in larl lifewire for a previous project I did what everyone in the comments was expecting to do you kind of syn the data
from the server into the JavaScript using Alpine you can kind of entangle all of that and basically when a new to-do is pushed to that JavaScript array then you just watch that within lville Live Wire and then it pushes it back to the database or wherever you're saving that information well that's definitely one way of doing this but in the video Caleb said something really interesting he said now you can JavaScript out this
whole thing so that everything happens in JavaScript and then the back end just synchronizes but we're using Live Wire and we want like an easier life than that and there's actually a few little tricks to make this work surprisingly well that I'm pretty stoked about so that got me thinking how can you do something like this with lay of a Livewire without doing it the way I would expect to do it I literally look through every single page of the Livewire documentation imagining what
would be the best way to do this if I didn't touch Alpine or at least like JavaScript Alpine hacks my inial thought was life life cycle hooks being able to uh say okay when this is updating maybe I call that information and view it on the page until it's updated and you would think that would work but updating still is waiting for the server so if there's a slow connection you have to wait until the server tells you that
it's actually updating but that's when I remembered the wire confirm element and it made me think a lot of the times within Lille Live Wire without diving too specific into how everything is working Caleb actuallyy has a fantastic course on layer casts about how he built Live Wire now wire confirm still works on slower internet so if you were to select slow 3G hit wire confirm you still have that confirmation dialogue
appear so you're not actually registering that wire confirm in on the server so what if there was some way of getting the the value that you want like you would have in a wire confirm you have access to values Live Wire does some incredibly magical things to kind of have those client side VAR variables ready for you what if we could take what
has already been rendered and access it before we actually push it to the server well there's one other wire colon directive that works fantastic and this is where my investigative nature kind of came to a close when I realized this could be perfect wire loading now if you're new to Livewire and you're new to Lille Live Wire in general wire loading basically allows you to say anytime one
of those requests is happening you can be more specific to say let's say it's an add to-do request we can show something within an element an HTML element using this wire loading directive but there's also some other things wire loading remove so display something but remove it when that same method on the server in Live Wire land
is being run and you can do other crazy things like being able to talking classes and toggling attributes and of course like I said you can Target specific actions and even one step further you can Target um action parameters saying I only want this to Target when you're removing a particular post maybe now you're kind of getting one step ahead of me saying okay I understand how you do it well let's dive
into the code okay well let's dive into the code here and I'll show you exactly how I think Caleb was doing doing it in this particular demo and probably one of the best ways of doing this if you're looking to have this particular interaction within your own application I'll go ahead and hide my face so you can see more of the code this is a larable Livewire volt components that way we have just one single file you can see how everything's kind of working behind the scenes we have a pretty
standard to-do application we have an add to-do function creates the to-do adds it to the database and then resets the to do variable the ability to complete a to-do okay hide it or remove it from the list and then Lille Live Wire Vol we have this with function this basically is a Mount function in a larvo live wire component says okay every time this function reloads or every anytime
this Live Wire component reloads then we are going to show the to-dos grab all the to-dos that are not completed and show them to the user okay what does this look like so if we add a new to-do and I'm on slow 3G here I'm going to say hello it looks like everything is working incredibly
fast and it is because we're not actually showing the user everything because we're using wire loading behind the scenes to show them what we want them to see so let's go ahead and pull up our Network inspector so if I was to add it to do we see this is pending and then it completes but we don't see that here in
the client let's take a look add a new to-do hit enter we see that automatically appear what's happening well and this is the way that I decided how to do this there probably are other ways of still using wire loading to complete this task but this is what I thought would made the most sense let's take a look at the
input items or the to-do list first so here we have a list for each to-do we're going to Loop through the to-dos and this is basically just a a div that shows that uses the wire key and you'll see why we have wire loading remove here in a second this is the standard to-do list this wire click is the complete to-do as you can see here bam okay
well that's the N for each well let's take a look at why this wire loading remove is here well here's the where the fun starts to happen because we have a second div that says only when wire loading is happening when we have the wire Target of add to-do we are creating a whole new to-do behind the scenes with the information alpine x
text of wire to-do this wire to-do is automatically binded to this input model here and because this input model wire model it's not wire model live but anytime we send a request in this case a form that's automatically going to be synced within Alpine using Live Wire magical stuff behind the scenes so when we have this x
text wire to do when we're waiting when we're loading the full to-do to be completed within this l a blade you know for each loop we're going to actually take the text show it using Alpine and then show it in this span just to show exactly what's happening behind the scenes here let's just um let's go ahead and say that this is a background of uh red 400 so that way you can see exactly
what this looks like behind the scenes okay now if we were to go back and take a look you can see here that if we were to enter another to-do we see it load red again we're on slow 3G here and then it appears it shows the user again now this is a great way to then show your user whatever you want them to see of course it can be
optimistic in this particular sense where we have you know the the of course it can be optimistic in this particular sense where we have we to put this back to slate 50 and then be able to and then be able to show the optimistic result so it looks exactly like how you
would expect but you could maybe show a grade out version of this to say hey we're trying to you know push this to the database we're trying to save this but it's not necessarily fully saved yet now another question that kind of popped up was well what happens if for some reason it wasn't able to connect to the database if it wasn't able to save properly well great question instead of actually saving this to the database
here for add to do I'm just going to delete we'll just um we'll just delete this real quick and we'll just say we'll have a sleep of one okay so this add to do isn't doing anything we're just adding it to do well everything still works how we would expect in the sense that when I hit this we should show an optimistic results to the user but
when the database actually goes through when we hit our server and receive what we're expecting to from that add to-do Livewire method it just doesn't save now this is where we could show the user something of maybe when that you new Todo doesn't actually save now they get a a response they say okay actually this didn't save maybe I should try typing that again and of course if you're error handling properly you probably have the
ability to then show the user something saying hey we weren't able able to save that request now there's one other thing that was really neat about Caleb's demo that I don't know if I fully replicated but I gave it a shot and that was whenever he hit enter the input was automatically cleared now I don't know if this is the best way to do it but it's the only way that kind of came to
mind because yeah maybe you could just clear that live wire modeled variable so in this case wire model Todo I ended up just putting two inputs so the first input would then be removed when add to do is trying to submit to the server while the second input doesn't have this wire model so it's not actually displaying anything to the user it's
more of just like a placeholder and again there's probably different ways of doing this and maybe you know clearing the to-do variable that's being linked within life but that's the way that I kind of came up with so if this optimistic UI in Lille Live Wire was kind of a challenge put forth by Caleb and it probably wasn't but I feel like I at least accomplished it good enough for my own standards because now we have the
ability to show the user whatever we want to show them and in this case make it look like their request went through and they're not having to worry about their Network or having to worry about how slow their internet is and as an added Bose I got to dive in even deeper to the Laro Live Wire documentation so let me know how you would solve this maybe without touching JavaScript and then maybe there's a better way to do it with JavaScript