Closed srkunze closed 3 years ago
Will hx-swap="beforeend"
work for you?
Even if you would like to update the form itself, you could do
hx-target="myform"
hx-post="post_form"
and then you could return the form with the post swapped out of band using something like
<my form>
<newpost id="mylistofposts" hx-swap-oob="beforeend">
The most straightforward way to handle this in HTML would be to use out of band swaps:
https://htmx.org/docs/#oob_swaps
And send down the new list of things with an id and the hx-swap-oob
attribute set to true.
Alternatively, there is an extension, path-deps
that could address the situation using path-based dependencies:
https://htmx.org/extensions/path-deps/
this can be set up to trigger a new request from the list of things when the form posts to it
I know you said it isn't an option to replace
hx-preserve will preserve an element between swaps:
https://htmx.org/attributes/hx-preserve/
which can be used for things like videos or highly-state-dependent nodes.
Also, there is a morphdom extension that uses morphdoms extremely sophisticated DOM syncing algorithm:
https://htmx.org/extensions/morphdom-swap/
Out of band swaps is probably the most straight forward, but there are many ways to skin this cat. :)
@srkunze does the above answer your use case?
@1cg Still thinking about it. I didn't have the oob-option on my radar so far; thus I'm still evaluating it. My gut feeling (and that's why it have been taking so long to answer) is ambiguous, i.e. feels interesting but not quite right.
In order to approach my issue, I proceeded by trying to work-around the limitations so far and get a feeling about HTMX as well as thinking through more conceptual issues here. For this purpose, I opened #375 and #376 for these separate ones.
From an eagle's perspective (and I remember the discussion with colleagues), this is a data-dependency issue. I'm going to explain what I mean by that:
L
displays several items drawing data from sources A
and B
(let's say PostgreSQL tables).C
adding data to source A
S
displaying statistics from source A
M
in L
to modify an item, thus changing data in source B
Our requirements:
L
, M
and S
to represent the current state of the data sources A
and B
C
know about M
? rather not)As a consequence:
C
is submitted and succeeds, we need to update L
and S
(M
implicitly by L
)M
is submitted and succeeds, we need to update L
In a discussion with a friend of mine, we discovered that websockets
should solve that issue more gracefully for the following reasons:
Combining this with the morphdom extension would also be really cool. I'll keep you posted.
One comment about out-of-band swaps: here we have the ability to replace data-related elements by an request to URL U
. The main issue happens if somebody introduces changes.
Code-wise, all OOB-providing URLs (including U
) must be discovered and updated. That holds for new elements, existing elements or elements to be removed from the source. So, managing this process can become quite hard.
One comment about path-deps
: my gut-feeling is quite good here. I would backtrace to use it when the websockets
idea fails.
path-deps
fully solves requirement 2, partially requirement 1 (only for the current user - not for all). Still it has the re-implementation issue.
@BoPeng @1cg thanks for your feedback so far. 🙂️
@BoPeng @1cg
My tests using hx-ws were quite successful and also code-wise highly maintainable. I opened pull requests #381 #382 since I noticed some missing features (helpful for development and production).
I know that the original issue has not been solved fully without websockets but I have the feeling that in the environment of current technologies, we should not fight the progress and embrace it.
So, I would rather close this issue here. Except, you want to leave it open for later adding this functionaliy.
If websockets are working for you, then let's go with that.
Still waiting on a review of your change and then we'll merge it. Thanks!
Hello, @1cg and @srkunze !
I found myself today with the very same problem.
Would something like the following code make sense?
First, we use the functionality that already exists, attaching a listener to the form.
<ul
hx-get="/from/l"
hx-trigger="event-after-form-submit from:form.just-submited"
hx-target="#l_container"
></ul>
We could do the same to another list.
<ul
hx-get="/from/another"
hx-trigger="event-after-form-submit from:form.just-submited"
hx-target="#another_container"
></ul>
Finally, we tell HTMX to dispatch a special custom event after swap (could also choose another events that make sense).
<form hx-post="/my-post" hx-dispatch-after-swap="event-after-form-submit">
</form>
I tried the path-deps
extension. It works, but, depending on the page, you would 'intercept' paths that you do not want. It was my case.
If something like I described as a possible approach existed, it would be cleaner and less likely to interfere with other stuff on the page.
UPDATE
I missed the hx-ext="ignore:path-deps"
possibility at first. I used it now to get rid of interference between contexts.
@ribeiropaulor Custom events sound really cool. I don't know if hx-target
is really necessary but hx-dispatch-after-swap
seem to do the trick here.
@1cg What is your opinion on this?
The preferred way of doing this would be to use the htmx events, either with vanilla javascript or hyperscript.
Here's what the hyperscript would look like:
<form hx-post="/my-post"
_="on htmx:afterSwap trigger event-after-form-submit">
...
</form>
The preferred way of doing this would be to use the htmx events, either with vanilla javascript or hyperscript.
Here's what the hyperscript would look like:
<form hx-post="/my-post" _="on htmx:afterSwap trigger event-after-form-submit"> ... </form>
Not to be obtuse, but this is something I've been trying to sort out as I adopt htmx for a project. How would this be used to clear a format after a submit? would the event-after-form-submit
be a function that cleared the form?
Hi everybody, a little bit of a background from my side: I worked at a company where we build a similar framework like htmx for internal usage. So I was excited to see the very same idea in the wild last year. Then I decided to use htmx for a small side-project of mine.
Looking at https://htmx.org/examples/ and sifting through the reference however it became clear that there was one important use-case not obviously being covered (or I'm didn't find it in the docs). Think of the following situation in pseudo-html:
When the create-form successfully created a new object, the
list of things
should be updated as well as thecreate-form
being reset for yet another post. And the rest of thestuff
,body
orhtml
should not be touched or triggered.This brings me to the proposal:
Reloading multiple others hx-elements by triggering them.
Or alternatively:
Let's add an example for a create-form with updating other elements after successful form submission (if that's already possible).