wire:click"doSomething; $emit('event')" possible?

What seems to be the problem:
I try to have wire:click do two things:

  • Call doSomething()
  • and emit the event ‘event’

Steps to Reproduce:
Well, implement something like:

wire:click="addToCart(); $emit('updateCartCount')"

Are you using the latest version of Livewire:
Yes

When I try to do it that way, I get errors in the console.

I don’t believe there’s a way to fire two different things inline like that. Two wire:clicks might work, but I doubt it. You could set up a javascript listener that fires both off, but you’re better off to add $emit(‘updateCartCount’) to the end of your addToCart method because I have a strong feeling race conditions are going to affect what you are trying to do here.

1 Like

Yes you are right, currently I had it setup exactly as you suggested.

And after all it will do two requests, no matter what. The only thing that could be optimized is the roundtrip wait for the event to happen.

Because:
AddToCart is first,
after that the UpdateCartCount request will fire.

However, let me try to not nitpicking here about speed, because we are not in production yet. :grin:

xxdalexx many thanks for your thoughts!

If your UpdateCartCount is triggering a method that is reaching into the database for the count, you could put the count into your session in addToCart(), and just read the session instead of going into the database. It would be a slight performance increase at least.

Luckily that is already in the session, thanks!