Open josh-tt opened 1 year ago
Interesting.
I think the teleport should just be handled by the x-teleport
wrapping the x-ajax, and I think this adds a lot of extra logic it doesn't need. For example, we don't need to clone the new children or remove them from anywhere, since they are brand new disconnected nodes already.
To support children we probably only need to make the
if (selector.tagName == 'BODY') return el.replaceChildren(...selector.children);
into
if (selector.tagName == 'BODY' || modifiers.includes('children')) return el.replaceChildren(...selector.children);
for getting all children children of an all selector
modifiers.includes('all') ? doc.body.querySelectorAll(query) : doc.body.querySelector(query)
// to
modifiers.includes('all') ? doc.body.querySelectorAll( modifiers.includes('children') ? query+'>*' : query) : doc.body.querySelector(query)
That should handle those cases.
Want to test it and make that PR?
Nice one, thanks. I will check that out soon and see if I can make sense of it and let you know. I've been experimenting with x-ajax to do some 'load more/infinite scroll and carousel' components from php generated 'paginated pages' from the cms.X ajax in the content lazily for each page into a component and then loop over it using x-for to do stuff. Didn't think it would work so well, but it's quite smooth so far.
That's good to hear!
I have not though about actually using this one in production like I have the others.
But this does give me some ideas... 🤔
Yeh I love it. Lots of different problems it can solve with few lines and it makes life easy (or at least more interesting). I'm using wordpress (which I hate) so finding ways not to use wordpress while still using wordpress is apparently my mission...
For example using it with query strings on the server side means I can grab different templates depending on the context where I'm calling from, but keep the templates in one place. Like if you have some latest posts that you want to show on the home page with a special look and feel, you just make the home page article template on your blog page accessible with a query condition 'context=home'. All the data is on the blog page already so that's easy to build a lot of variations with the same data.
Or something like a 'minacart'. Build the mini cart on the cart page behind a query string condition, then when you want to get the mini cart or refresh it you can just call it with x-ajax. And it's easy to refresh it with events.
It's also good for data as well. I made a command palette style search with the load more/infinite scroll component. Fetch all the posts lazily, query select and extract text content and build an array of data from the content to search/filter.
I think I'm abusing it a quite a bit, but it makes things simple and is a single solution for a lot of different things.
Hi Erik, I tried to submit some changes, but not sure they ever went through.
In any case, it needed some more work and effort try to cover all the cases. This is what I have if you'd like to check it out and seems to be doing the trick, though required a bit more code.
There is a test file which should help to illustrate things. Just note the directive is named ajax-mod to run alongside the original for testing purposes.
Src: https://github.com/josh-tt/tt-x-ajax-mod/blob/main/x-ajax-mod.js Tests setup: https://github.com/josh-tt/tt-x-ajax-mod/blob/main/x-ajax-test.html
Hi Eric, other day I found a need for children and saw it was in your todo. I took a stab at it and also added some other bits (teleport and transitions). In the end I bailed on it as it was easier to just handle things in vanilla, so forgive the mess, but worked ok. Thought I'd leave it here in case there are some ideas for you. Hope you're well `/**
export default function (Alpine) { Alpine.directive('ajax', async (el, { expression, modifiers }, { effect, evaluateLater }) => { const target = evaluateLater(expression); let query;
}
const eventDefaults = { bubbles: false };`