HTMX trigger custom event after load - htmx

Imagine a tabbed panel. I do some changes to data in tab1, and I use HTMX to POST and swap content in tab1 as I go. Now, there may be parts of the information in tab1, where when I change it, I must also refresh the contents of tab2.
I imagine setting tab2 up with hx-trigger="my-custom-event-when-tab-1-updates-the-special-place". All good. But now I'd like to inform HTMX to raise this event after the load/swap of a particular part of tab1.
How would I do that?
Or is there a different approach that would be easier to do in these cases?
It is basically a matter of handling side-effects in the UI. Yes, you did a thing, but now other things also needs to happen.

The simplest way would be to trigger the event server side when you return the content for tab 1. You can find more information in the htmx documentation: https://htmx.org/headers/hx-trigger/
You would need to change the trigger on tab2 to listen my-custom-event-when-tab-1-updates-the-special-place from:body.
Another approach would be to use the htmx:afterSwap event. To do this you would need to listen to the event on tab1, then manually trigger the event on tab2.

Related

htmx: Update parent-Fragment if child-form is valid

I am learning the "html fragments over the wire" approach of htmx.
I have a "parent" tag in my HTML page which contains a child which is a form.
Case1: If the child form gets submitted and is not valid, then I want to re-display the child form (hx-swap="outerHTML"). This works fine.
Case2: if the child form gets submitted and is valid, then I want to re-fresh the parent.
I had a look at hx-swap-oob, but this needs to be an attribute of the HTML send from the serve to the client.
This is possible, but it is a bit dirty to get the attribute into the parent tag.
Is there a way to trigger something like oob, but why http-header?
Depending on the rest of your website, I'd suggest using the hx-trigger response header to trigger a new event on your website when your form is posted successfully. You can use htmx, hyperscript, javascript, or anything else listen for this event and take the necessary action.

Allow natural back button behaviour with use case of dynamically-added query parameters

I have the case that I push some parameters to the nuxt router (https://router.vuejs.org/guide/essentials/navigation.html) whenever somebody visits a page without any parameters.
Example:
somebody visits: /program it will end up in /program/first-event?year=2018&month=6
(First the view filters the program for current events (therefore the parameters for this year and this month) and then from the filtered events it will set the first event as active post (also by pushing it to the router).
This is all wanted and good. BUT now I detected the following problem:
Somebody is visiting /aboutus and then navigates to /program, the router will automatically change to /program/url-of-event-post?year=2018&month=6.
Assuming the user wants to go back to /aboutus he clicks on the browser back button. This will bring him back to: /program which automatically adds the post and the parameters again (effectively moving one step forward again).
Means the user is caught in clicking endlessly on the back button.
My approach would be to try to register if a user clicks on the back button and if so, I would not add the parameters. But I don't know how to do this.
I thought the router would provide some 'from' property, but so far I did not find anything.
I would be very happy to hear some thoughts on this. Thank you heaps in advance.
You need use router.replace.
https://router.vuejs.org/guide/essentials/navigation.html#router-replace-location-oncomplete-onabort

GWTP - Identify when Presenter is removed from Slot

I want to add a confirmation popup when a user navigates away from one of my presenters and is replaced in a NestedSlot. Can I intervene before a place is revealed and check the current presenter?
Edit: I just learned that the PlaceManager has some support for this using the setOnLeaveConfirmation method. That said, I still don't think this will work for my case because I want the confirmation popup to be associated with a single nested presenter. I would also prefer to manually intervene because I already have a confirmation modal for a cancel button that I want to reuse.
It would have been simple if you could override window.confirm() using GQuery the same way as you can in JQuery, however that's not the case. Your best option is still using placeManager.setOnLeaveConfirmation(). You could probably emulate the same behaviour as window.confirm() with a PopupWidget, but then it wont block access to other parts of the page.

Changing CSS variables based on JavaScript events

We have a Facebook app here which has three boxes with three buttons. Each button, when clicked, should change state once an event (successfully) happens. The buttons are almost all working (a few minor bugs) but they have been done a rather long-winded and inefficient way.
My question is, what is the most efficient way to link events (i.e. page 'liked', personal information submitted, page 'shared') with the state of the buttons?
You can use jQuery and the .trigger() function to fire off events. Then you can use .bind() to listen to the events and do something. For example, when the user logs in, you can trigger a user_logged_in_event, and bind to it. In the function, you can then use JS to change the login button to logout...

Where can I read how to make web notifiers such as the StackExchange at the top left side of StackOverflow screen?

I'm not even sure what the name of that is to be able to make a search... but I would like to make those kind of things. Facebook has that too with the messages, notifications and friends requests. Thanks
I'm not sure if you expect anyone to give you a complete tutorial with source code included? :) You should probably do some digging around yourself, since a concrete answer on this could mean to write a few pages :)
How can you dig around?
Thé tool for a job like that is Firebug (IMO).
With bigger tasks like these it makes sense to try to split it up in smaller pieces.
Let's say you go for a widget like the user profile popup on SO.
you need some HTML to display in a popup: right click on any html element on the popup and click the 'inspect element' menu item. This brings you to the HTML tab in firebug. This allows you to figure out how the HTML is structured
you need some CSS to style that popup: when you're browsing the html structure, you might already have noticed that on the right side of it is the CSS that is applied to the active element
you might want to use some animation effects: for that you could use jquery. Have a look here to find out more on which effects are available and how they can be triggered. Fading is used in the profile popup on SO.
then you might ask yourself the question where SO get's that html structure from, right? To find out more about which server calls are made you can use the 'NET' tab in Firebug. (When you hover over your user name (only the first time?), then you should notice there's a call made to something like: http://stackoverflow.com/users/profile-link-stats?_=someLongNumberHere
In firebug you can then inspect the request and response. You should notice that the response is some HTML structure. This HTML structure is then inserted into the DOM.
Sooooo you can kinda glue it all together now:
the user hovers over his user name
the hovering triggers a server call (see step 4): use jquery hover to attach a handler to the user link. (subsequent hovers don't trigger that server call, so there needs to be a check to see if that profile popup was already loaded or not)
when the server call successfully returns (see jquery get), the returned html is inserted into the DOM and a fadeIn effect is triggered.
it seems a mouseout is used to fadeOut the popup
I HOPE this is the answer you were looking for. It took me a while ;)
You probably need to check out stackapps