forked from bigskysoftware/htmx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add extension to populate path variables with request parameters (big…
…skysoftware#1307) * add extension to populate path variables with request parameters * update extension readme
- Loading branch information
Showing
5 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
htmx.defineExtension('path-params', { | ||
onEvent: function(name, evt) { | ||
if (name === "htmx:configRequest") { | ||
evt.detail.path = evt.detail.path.replace(/{([^}]+)}/g, function (_, param) { | ||
var val = evt.detail.parameters[param]; | ||
delete evt.detail.parameters[param]; | ||
return val === undefined ? "{" + param + "}" : encodeURIComponent(val); | ||
}) | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
describe("path-params extension", function() { | ||
beforeEach(function () { | ||
this.server = makeServer(); | ||
clearWorkArea(); | ||
}); | ||
afterEach(function () { | ||
this.server.restore(); | ||
clearWorkArea(); | ||
}); | ||
|
||
it('uses parameter to populate path variable', function () { | ||
var request; | ||
htmx.on("htmx:beforeRequest", function (evt) { | ||
request = evt; | ||
}); | ||
var div = make("<div hx-ext='path-params' hx-get='/items/{itemId}' hx-vals='{\"itemId\":42}'></div>") | ||
div.click(); | ||
should.equal(request.detail.requestConfig.path, '/items/42'); | ||
}); | ||
|
||
it('parameter is removed when used', function () { | ||
var request; | ||
htmx.on("htmx:beforeRequest", function (evt) { | ||
request = evt; | ||
}); | ||
var div = make("<div hx-ext='path-params' hx-get='/items/{itemId}' hx-vals='{\"itemId\":42, \"other\":43}'></div>") | ||
div.click(); | ||
should.equal(request.detail.requestConfig.parameters.other, 43); | ||
should.equal(request.detail.requestConfig.parameters.itemId, undefined); | ||
}); | ||
|
||
it('parameter value is encoded', function () { | ||
var request; | ||
htmx.on("htmx:beforeRequest", function (evt) { | ||
request = evt; | ||
}); | ||
var div = make("<div hx-ext='path-params' hx-get='/items/{itemId}' hx-vals='{\"itemId\":\"a/b\"}'></div>") | ||
div.click(); | ||
should.equal(request.detail.requestConfig.path, '/items/a%2Fb'); | ||
}); | ||
|
||
it('missing variables are ignored', function () { | ||
var request; | ||
htmx.on("htmx:beforeRequest", function (evt) { | ||
request = evt; | ||
}); | ||
var div = make("<div hx-ext='path-params' hx-get='/items/{itemId}/{subitem}' hx-vals='{\"itemId\":42}'></div>") | ||
div.click(); | ||
should.equal(request.detail.requestConfig.path, '/items/42/{subitem}'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
+++ | ||
title = path-params | ||
+++ | ||
|
||
This extension uses request parameters to populate path variables. Used parameters are removed so they won't be sent in the query string or body anymore. | ||
|
||
## Install | ||
|
||
```html | ||
<script src="https://unpkg.com/htmx.org/dist/ext/path-params.js"> | ||
``` | ||
## Usage | ||
This would invoke URL `/items/42?foo=bar` | ||
```html | ||
<div hx-ext="path-params"> | ||
<a hx-get="/items/{itemId}" hx-vals='{"itemId": "42", "foo": "bar"}'>test</div> | ||
</div> | ||
``` |