mirror of
https://github.com/bigskysoftware/htmx.git
synced 2025-10-02 07:21:05 +00:00
Merge pull request #287 from jvosloo/patch-1
Extend path-deps extension to handle explicit path refreshes
This commit is contained in:
commit
522b69c3b7
@ -1,4 +1,9 @@
|
|||||||
(function(){
|
(function(undefined){
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
// Save a reference to the global object (window in the browser)
|
||||||
|
var _root = this;
|
||||||
|
|
||||||
function dependsOn(pathSpec, url) {
|
function dependsOn(pathSpec, url) {
|
||||||
var dependencyPath = pathSpec.split("/");
|
var dependencyPath = pathSpec.split("/");
|
||||||
var urlPath = url.split("/");
|
var urlPath = url.split("/");
|
||||||
@ -15,21 +20,39 @@
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function refreshPath(path) {
|
||||||
|
var eltsWithDeps = htmx.findAll("[path-deps]");
|
||||||
|
for (var i = 0; i < eltsWithDeps.length; i++) {
|
||||||
|
var elt = eltsWithDeps[i];
|
||||||
|
if (dependsOn(elt.getAttribute('path-deps'), path)) {
|
||||||
|
htmx.trigger(elt, "path-deps");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
htmx.defineExtension('path-deps', {
|
htmx.defineExtension('path-deps', {
|
||||||
onEvent: function (name, evt) {
|
onEvent: function (name, evt) {
|
||||||
if (name === "htmx:afterRequest") {
|
if (name === "htmx:afterRequest") {
|
||||||
var config = evt.detail.requestConfig;
|
var config = evt.detail.requestConfig;
|
||||||
// mutating call
|
// mutating call
|
||||||
if (config.verb !== "get") {
|
if (config.verb !== "get") {
|
||||||
var eltsWithDeps = htmx.findAll("[path-deps]");
|
refreshPath(config.path);
|
||||||
for (var i = 0; i < eltsWithDeps.length; i++) {
|
|
||||||
var elt = eltsWithDeps[i];
|
|
||||||
if (dependsOn(elt.getAttribute('path-deps'), config.path)) {
|
|
||||||
htmx.trigger(elt, "path-deps");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
})();
|
|
||||||
|
/**
|
||||||
|
* ********************
|
||||||
|
* Expose functionality
|
||||||
|
* ********************
|
||||||
|
*/
|
||||||
|
|
||||||
|
_root.PathDeps = {
|
||||||
|
|
||||||
|
PathDeps.refresh: function(path) {
|
||||||
|
refreshPath(path);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}).call(this);
|
||||||
|
@ -138,5 +138,24 @@ describe("path-deps extension", function() {
|
|||||||
div.innerHTML.should.equal("Deps fired!");
|
div.innerHTML.should.equal("Deps fired!");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('path-deps api basic refresh case works', function () {
|
||||||
|
this.server.respondWith("GET", "/test", "Path deps fired!");
|
||||||
|
var div = make('<div hx-get="/test" hx-trigger="path-deps" path-deps="/test">FOO</div>')
|
||||||
|
PathDeps.refresh("/test");
|
||||||
|
this.server.respond();
|
||||||
|
div.innerHTML.should.equal("Path deps fired!");
|
||||||
|
});
|
||||||
|
|
||||||
|
it('path-deps api parent path case works', function () {
|
||||||
|
this.server.respondWith("GET", "/test1", "Path deps 1 fired!");
|
||||||
|
this.server.respondWith("GET", "/test2", "Path deps 2 fired!");
|
||||||
|
var div = make('<div hx-get="/test1" hx-trigger="path-deps" path-deps="/test/child">FOO</div>')
|
||||||
|
var div2 = make('<div hx-get="/test2" hx-trigger="path-deps" path-deps="/test">BAR</div>')
|
||||||
|
PathDeps.refresh("/test/child");
|
||||||
|
this.server.respond();
|
||||||
|
div.innerHTML.should.equal("Path deps 1 fired!");
|
||||||
|
this.server.respond();
|
||||||
|
div2.innerHTML.should.equal("Path deps 2 fired!");
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
@ -39,6 +39,23 @@ You can use a `*` to match any path component:
|
|||||||
</div>
|
</div>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Javascript API
|
||||||
|
|
||||||
|
### <a name="refresh"></a> Method - [`PathDeps.refresh()`](#refresh)
|
||||||
|
|
||||||
|
This method manually triggers a refresh for the given path.
|
||||||
|
|
||||||
|
##### Parameters
|
||||||
|
|
||||||
|
* `path` - the path to refresh
|
||||||
|
|
||||||
|
##### Example
|
||||||
|
|
||||||
|
```js
|
||||||
|
// Trigger a refresh on all elements with the path-deps attribute '/path/to/refresh', including elements with a parent path, e.g. '/path'
|
||||||
|
PathDeps.refresh('/path/to/refresh');
|
||||||
|
```
|
||||||
|
|
||||||
#### Source
|
#### Source
|
||||||
|
|
||||||
<https://unpkg.com/htmx.org/dist/ext/path-deps.js>
|
<https://unpkg.com/htmx.org/dist/ext/path-deps.js>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user