https://htmx.org logo
Join Discord
Powered by
# htmx-general
  • b

    blue-gold-89534

    12/01/2022, 10:31 AM
    All my editors have an id with
    id="editor-xxxx"
    set and I did:
    Copy code
    js
    hx-trigger="every 30s [!document.querySelectorAll(`[id^='edit']`).length"
  • b

    blue-gold-89534

    12/01/2022, 10:40 AM
    works like a charm 🙂
  • r

    ripe-action-67367

    12/01/2022, 10:44 AM
    Great!
  • m

    mysterious-toddler-20573

    12/01/2022, 12:13 PM
    might be worth extracting that out to a method to clarify:
    Copy code
    html
    <script>
      function whenNotEditing() {
        return !document.querySelectorAll(`[id^='edit']`).length
      }
    </script>
    ...
    <table ... 
           hx-trigger="every 30s [whenNotEditing()]"
  • b

    blue-gold-89534

    12/01/2022, 12:23 PM
    you are obviously right!
  • b

    blue-gold-89534

    12/01/2022, 12:24 PM
    maybe a method like
    hx-StopParentTrigger
    would be a neat addition in htmx2.0?
  • m

    mysterious-toddler-20573

    12/01/2022, 12:26 PM
    yes, that sounds reasonable
  • b

    bitter-magician-10249

    12/01/2022, 12:47 PM
    Absolutely loving HTMX and Hyperscript, such a breath of fresh air compared to the SPA frameworks I've worked in previously. Most of the pages are easily adaptable to the server-side rendered way of working but I was wondering how some of you would implement a page on which you show Google Maps with a number of assets as a marker on the map. The side content (next to the map) would list the assets and their "last seen" datetime property for instance. Updating the side content is easy with the "every 10s" trigger, but how would you update the position of the marker? A full page refresh does actually seem overkill here as the map initialization is a heavy process (certainly for mobile devices). One possible solution I thought of is to send the JSON serialized version of the assets (as you would do in an API-based approach) as a header, and then retrieve that and invoke a regular javascript function to update the markers, but it feels quite dirty. Interested to hear how other people would approach this.
  • m

    mysterious-toddler-20573

    12/01/2022, 12:50 PM
    what sort of user interaction updates the map? Dragging it?
  • b

    bitter-magician-10249

    12/01/2022, 12:50 PM
    no interaction, let's say the assets send a periodic ping to the server, which updates their latitude/longitude coordinates
  • m

    mysterious-toddler-20573

    12/01/2022, 12:51 PM
    Ah, got it.
  • m

    mysterious-toddler-20573

    12/01/2022, 12:51 PM
    So, in that case, I might use a poll on the side content and an associated HX-Trigger response header (w/ a JSON payload) to update the markers
  • m

    mysterious-toddler-20573

    12/01/2022, 12:52 PM
    keep it all in one server request that way
  • b

    bitter-magician-10249

    12/01/2022, 12:54 PM
    right, but the side content is HTML and would be updated as well (to show the last updated datetime for instance)
  • b

    bitter-magician-10249

    12/01/2022, 12:55 PM
    so you'd have one poll for the HTML content and have it add a trigger and then have a request which is JSON, to update the markers
  • m

    mysterious-toddler-20573

    12/01/2022, 12:55 PM
    yes
  • m

    mysterious-toddler-20573

    12/01/2022, 12:55 PM
    You could do them both in a single request
  • m

    mysterious-toddler-20573

    12/01/2022, 12:55 PM
    the poll gets the new side content and replaces it, and then that response also includes an
    HX-Trigger
    response header, that triggers an event (e.g.
    updateMarkers
    ) and includes the JSON payload needed to do so
  • m

    mysterious-toddler-20573

    12/01/2022, 12:56 PM
    am I making any sense?
  • b

    bitter-magician-10249

    12/01/2022, 12:57 PM
    but doesn't the HX-Trigger header only contain the name of the event to be triggered, where does the JSON payload come from?
  • m

    mysterious-toddler-20573

    12/01/2022, 12:58 PM
    You can include payloads in it
  • b

    bitter-magician-10249

    12/01/2022, 12:58 PM
    oh ok, didn't know that
  • m

    mysterious-toddler-20573

    12/01/2022, 12:58 PM
    https://htmx.org/headers/hx-trigger/
  • b

    bitter-magician-10249

    12/01/2022, 12:59 PM
    neat, that looks like a clean solution
  • m

    mysterious-toddler-20573

    12/01/2022, 12:59 PM
    Copy code
    HX-Trigger: {"updateMarkers":[{marker1: blah blah blah}]}
  • m

    mysterious-toddler-20573

    12/01/2022, 12:59 PM
    not awful, anyway
  • b

    bitter-magician-10249

    12/01/2022, 12:59 PM
    thanks for the help!
  • s

    stocky-dentist-80693

    12/01/2022, 1:42 PM
    In the past, I've done a similar thing by having JSON-encoded coords in
    data-marker
    attributes on the HTML items, and then have a JS function that parses the items for their
    data-marker
    attributes and update the map accordingly. The JS function is called both on page load/maps init as well as for htmx responses.
  • b

    bitter-magician-10249

    12/01/2022, 2:10 PM
    @stocky-dentist-80693 , not a bad idea either!
  • b

    bitter-magician-10249

    12/01/2022, 2:51 PM
    might even be necessary, as the data that can be contained in an HTTP header is probably limited (I'm reading something like 8KB max for some web servers), embedding it as data-* attributes in the HTML wouldn't have this kind of limitation
1...929930931...1146Latest