htmx-extensions icon indicating copy to clipboard operation
htmx-extensions copied to clipboard

loading-states.js `data-loading-states` scope not working properly

Open Odas0R opened this issue 1 year ago • 3 comments

// <a> is boosted
<a href="/path"

// <a> request must not affect elements inside this div
// but it is, at the moment. All elements inside this div
// are affected by the outside <a> GET request
<div data-loading-states>
  <form
  hx-post="/api/v1/auth/sign-in"
  hx-trigger="submit">
  ...
  </form>
</div>

The problem is in:

function loadingStateContainer(target) {
  return htmx.closest(target, "[data-loading-states]") || document.body;
}


function getLoadingStateElts(loadingScope, type, path) {
  return Array.from(htmx.findAll(loadingScope, "[" + type + "]")).filter(
    function (elt) {
      return mayProcessLoadingStateByPath(elt, path);
    },
  );
}

The fix:

function loadingStateContainer(target) {
  return htmx.closest(target, "[data-loading-states]") || document.body;
}

function getLoadingStateElts(loadingScope, type, path) {
  return Array.from(htmx.findAll(loadingScope, "[" + type + "]")).filter(
    function (elt) {
      return (
        mayProcessLoadingStateByPath(elt, path) &&
        // ensure that the loadingScope is the same as the specified scope
        loadingStateContainer(elt) === loadingScope 
      );
    },
  );
}

Odas0R avatar Aug 19 '24 17:08 Odas0R

Hey, feel free to submit bugfix pull requests directly!

Telroshan avatar Aug 20 '24 18:08 Telroshan

Hey, feel free to submit bugfix pull requests directly!

Hey, I'm playing with it. Stumbled on #78 too. Will fix it, and after some tinkering, will open a PR.

This can stay open just for info purposes.

Odas0R avatar Aug 22 '24 21:08 Odas0R

Found that loading state is still activated when cached responses are being used (in the browser). Also might have some suggestions in the future for the API

Odas0R avatar Aug 23 '24 08:08 Odas0R