در حال بارگذاری ...

Handling @click Expressions0:00

So now our Alpine component is reactive, we can do data count equals 4 and it reacts and turns to 4. So the next step is making it reactive based on our expressions in the HTML, like @click. So when I click, I want something to happen. I want 4 to turn into 5. So what we need to do is register event listeners, and we're going to do it the same way we did the refreshDOM. So when we do refreshDOM, we walk through each element and we look for things and then we do things based on what we find. We're going to use that same exact pattern for registering listeners.

Separating Listener Registration0:32

we do things based on what we find. We're going to use that same exact pattern for registering listeners. So with our refreshDOM, think about it, we could actually register these listeners inside of here. As we're walking, we're checking for xtext, we could also check for click, you know, and then register a listener. But if you think about that, every time the data changes, it would re-add all these listeners, which is pretty hard to manage. So we're going to create a dedicated function called registerListeners that's only going to run up front.

Implementing registerListeners Walk0:58

So we're going to create a dedicated function called registerListeners that's only going to run up front. It's not going to run when data changes, it's just going to run on load. So let's create that function, registerListeners, okay? And now we're going to use that same strategy of walking through the DOM, starting at the root, and now as we hit elements, we're basically going to do the same kind of thing we did in refreshDOM. We're going to say if l has attribute atClick, and then register that listener. So if l has attribute atClick, okay, then here's the expression, getAttributeAtClick, and instead of setting inner text, we're going to register an event listener, l.addEventListener,

So if l has attribute at click, okay, then here's the expression, getAttributeAtClick, and instead of setting innerText, we're going to register an event listener, l.addEventListener, listen for a click. When that click is heard, we're going to just evaluate whatever expression is inside of there. Okay, makes sense. To recap, as soon as the whole page loads, we're going to walk through the DOM and register these listeners. We're looking for at click. If we find it, we get the value of it, which is count++, that's going to be the expression.

Generalizing to Any Event2:30

We have a totally functional component. So this is another milestone in the series. We've basically implemented how this works in Alpine. We've mirrored the functionality exactly, but with our own code, and this is it. That's the entirety of the code, the entire thing. So I think we're not done yet. I think the next thing we should do is make it flexible. Right now it's at click. Let's make it also accept mouseEnter and basically any event you pass in here. And also we could add other directives like show and things like that.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟