dani94's avatar
Level 1

Pass data throw component using AlpineJS

Hey guys!

Its my first time using AlpineJs, so im not really sure how to do this...

How i can pass dynamic datas to others components? In this example I need $product->id

@foreach($products as $product)
<button x-on:click.prevent="$dispatch('open-modal', 'confirm-product-deletion')">Delete</button>
@endforeach
<x-modal name="confirm-product-deletion">
    <form method="post" action="{{ route('product.delete', 1) }}">
	...
    </form>
</x-modal>

I would like to get desired route for every productId

0 likes
2 replies
martinbean's avatar

@dani94 What does the code look like in your x-modal component that listens for events to actually open the modal? You should be able to get a reference to the triggering element there.

For example, I use Bootstrap and its Modal component a lot. I have forms in my application to delete records:

<x-form :action="route('product.destroy', compact('product'))" method="DELETE" x-confirm="Are you sure you want to delete this product?">
    <button type="submit">Delete</button>
</form>

I then have an Alpine plugin (what you see as the x-confirm directive) that intercepts submissions and instead shows a modal:

import { Alpine as TAlpine } from 'alpine-js';
import Modal from 'bootstrap';

export default function (Alpine) {
    Alpine.directive('confirm', function (el, { expression }) {
        el.addEventListener('submit', function (event) {
            event.preventDefault();

            const modal = Modal.getOrCreateInstance('confirm-delete-modal');

            modal.querySelector('form').setAttribute('action', event.target.getAttribute('action'));
            modal.querySelector('.modal-body-message').textContent = expression;

            modal.show();
        });
    });
}

It intercepts submit events, finds the #confirm-delete-modal on the page, and then sets the form’s action attribute to the original form’s action attribute value, and replaces the contents of .modal-body-message with the text that was in the x-confirm expression.

The confirm delete modal is then just a modal with a form, a confirmation message, and submit button:

<div aria-hidden="true" aria-labelledby="confirm-delete-modal-label" class="fade modal" id="confirm-delete-modal" tabindex="-1">
  <div class="modal-dialog">
    <div class="modal-content">
      <header class="modal-header">
        <h1 class="fs-5 modal-title" id="confirm-delete-modal-label">Confirm delete</h1>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </header>
      <div class="modal-body">
        <p class="mb-0 modal-body-message"></p>
      </div>
      <footer class="modal-footer">
        <x-form action="#" method="DELETE">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">No, keep</button>
          <button type="submit" class="btn btn-danger">Yes, delete</button>
        </x-form>
      </footer>
    </div>
  </div>
</div>
dani94's avatar
Level 1

@martinbean Thanks for reply.

I am using the breeze starter kit modal. I think is not possible to do it in this way... Maybe make more sense do something like your solution.

Please or to participate in this conversation.