Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add x-target response code modifiers #78

Merged
merged 7 commits into from
Jun 14, 2024
Merged

Add x-target response code modifiers #78

merged 7 commits into from
Jun 14, 2024

Conversation

imacrayon
Copy link
Owner

@imacrayon imacrayon commented Jun 9, 2024

This PR adds x-target modifiers you can use to change targets based on the response code returned from the server:

<form id="my_form" x-target x-target.302="_self" x-target.500="critical_error" action="/update" method="patch">

This form will only update itself when submitted, with two exceptions:

  1. If the server responds with a 302 status, the browser will perform a full page navigation to the new content
  2. If the server responds with a 500 status, Alpine AJAX will target an element with ID critical_error instead of my_form

You can match a whole class of status codes using the wildcard syntax: x-target.4xx will match any 400 class status.

You can also match both 400 and 500 class status codes using the special error modifier: x-target.error.

This PR also adds two new events for handling responses and requests: ajax:send and ajax:sent.

ajax:send fires just before a fetch call is made, $event.detail contains all of the options that will be passed to fetch so that you can override anything.

ajax:sent fires after a fetch call is made to complement the send event. $event.detail contains the response data from the request.

Resolves #73, #76

@imacrayon imacrayon merged commit 041720f into main Jun 14, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

x-target response code modifiers
1 participant