diff options
author | carson <carson@leaddyno.com> | 2020-05-23 17:06:20 -0700 |
---|---|---|
committer | carson <carson@leaddyno.com> | 2020-05-23 17:06:20 -0700 |
commit | 23a9a33486075160d8faa3b9dccf78fdccb61a93 (patch) | |
tree | 1e65303d8eceeed4967ba0a2f8428a112f4e551f /src/ext/path-deps.js | |
parent | 2996827806db4b569ece545fa2ca63614c43af9e (diff) | |
download | htmx-23a9a33486075160d8faa3b9dccf78fdccb61a93.tar.gz htmx-23a9a33486075160d8faa3b9dccf78fdccb61a93.zip |
`path-deps` extension
fixes https://github.com/bigskysoftware/htmx/issues/21
Diffstat (limited to 'src/ext/path-deps.js')
-rw-r--r-- | src/ext/path-deps.js | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/ext/path-deps.js b/src/ext/path-deps.js new file mode 100644 index 00000000..5eedced1 --- /dev/null +++ b/src/ext/path-deps.js @@ -0,0 +1,36 @@ +(function(){ + function dependsOn(pathSpec, url) { + var dependencyPath = pathSpec.split("/"); + var urlPath = url.split("/"); + for (var i = 0; i < urlPath.length; i++) { + var dependencyElement = dependencyPath.shift(); + var pathElement = urlPath[i]; + if (dependencyElement !== pathElement && dependencyElement !== "*") { + return false; + } + if (dependencyPath.length === 0 || (dependencyPath.length === 1 && dependencyPath[0] === "")) { + return true; + } + } + return false; + } + + htmx.defineExtension('path-deps', { + onEvent: function (name, evt) { + if (name === "afterRequest.htmx") { + var xhr = evt.detail.xhr; + console.log(xhr); + // mutating call + if (xhr.method !== "GET") { + var eltsWithDeps = htmx.findAll("[path-deps]"); + for (var i = 0; i < eltsWithDeps.length; i++) { + var elt = eltsWithDeps[i]; + if (dependsOn(elt.getAttribute('path-deps'), xhr.url)) { + htmx.trigger(elt, "path-deps"); + } + } + } + } + } + }); +})(); |