imacrayon / alpine-ajax

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

Use ajax:success to preload assets (js) #55

Closed jonnitto closed 6 months ago

jonnitto commented 7 months ago

If the response is ok, the event ajax:success get's fired. I use this event to search in the markup for missing Alpine JS Plugins. As the plugins must have been loaded before the markup appears on the page, it would be nice to have a preventDefault option with a function in details who can be called to continue the work.

Pseudo-Code

window.addEventListener('ajax:success', (event) => {
   event.preventDefault();
   const html = event.detail.response.html
   // do the needed stuff

   event.detail.render();
})

Like that It would be even possible the alter the markup:

window.addEventListener('ajax:success', (event) => {
   event.preventDefault();
   const html = event.detail.response.html
   // do the needed stuff

   event.detail.render(html);
})
imacrayon commented 7 months ago

This is a cool use case! I like that API too.

imacrayon commented 6 months ago

This was just release in v0.5 there's a very similar example to yours in the PR: https://github.com/imacrayon/alpine-ajax/pull/62

jonnitto commented 6 months ago

Awesome!!!! ❤️