imacrayon / alpine-ajax

An Alpine.js plugin for building server-powered frontends.
https://alpine-ajax.js.org
MIT License
664 stars 14 forks source link

x-target response code modifiers #73

Closed mattwigham closed 5 months ago

mattwigham commented 6 months ago

First off, love the plugin. Thanks so much for putting in the time.

Second, I’d love the ability to put modifiers on x-target based on the response code returned from the server, similar to htmx's response targets extension. Perhaps something like:

<form id="my-form" action="/update" method="patch" x-target="body" x-target.422="my-form">

Perhaps something like this could be accomplished with intercepting the ajax:merge event with some custom retargeting? Either way, the Alpine modifier syntax would be some wonderful ergonomics.

Thanks again.

imacrayon commented 6 months ago

Interesting! I hadn’t come across that extension before. It would be cool if we could hack this by intercepting ajax:merged, but unfortunately that event doesn’t allow the target to be changed.

I think I like where your idea is going. It might even be a better alternative to the current follow and nofollow modifiers, those modifiers have never really sat right with me, and maybe we could replace them with something like x-target.302="_top" which would reload the page if a form submission redirects.

mattwigham commented 6 months ago

Perfect!