putyourlightson / craft-spark

A real-time, template-driven hypermedia framework for Craft CMS.
MIT License
17 stars 0 forks source link

A couple of questions #13

Closed cloudgrayau closed 3 weeks ago

cloudgrayau commented 3 weeks ago

Support Request

Hello,

I am just looking at possibly replacing Sprig with Spark on a website I am developing. I know that most of these will rely on the actual Datastar framework, but I can't find any documentation on either of them on the Datastar docs.

1) Is there a JavaScript afterSwap 'event' equivalent, similar to:

2) I also have some custom AJAX running. Is there any option to run the returned HTML through Spark? Similar to the HTMX equivalent:

Thanks,

Plugin Version

No response

bencroker commented 3 weeks ago
  1. No, but there is data-on-load, which is triggered whenever the element is loaded into the DOM. You can use this if you need to trigger some JS after an element has been swapped in. See the docs on using external scripts.

    <div data-on-load="myfunction()"></div>
  2. Is there any reason you can’t fetch the content via Spark? This might work, but it’s generally advised to avoid such trickery when at all possible.

    ds.applyPlugins(element)
cloudgrayau commented 3 weeks ago

Yep, perfect. Thanks.

It appears with point 2, that the AJAX fetched content already triggers spark, which was my mistake.