bump version & disambiguate non-swaps from errors

This commit is contained in:
carson 2021-10-30 08:19:54 -06:00
parent a4f1dc8a49
commit e67e5ba41b
8 changed files with 41 additions and 15 deletions

View File

@ -1,6 +1,8 @@
# Changelog # Changelog
## [1.6.1] - 2021-11-??
## [1.6.0] - 2021-10-01 ## [1.6.0] - 2021-10-01
* Completely reworked `<script>` tag support that now supports the `<script src="...'/>` form * Completely reworked `<script>` tag support that now supports the `<script src="...'/>` form

View File

@ -35,7 +35,7 @@ By removing these arbitrary constraints htmx completes HTML as a
```html ```html
<!-- Load from unpkg --> <!-- Load from unpkg -->
<script src="https://unpkg.com/htmx.org@1.6.0" ></script> <script src="https://unpkg.com/htmx.org@1.6.1" ></script>
<!-- have a button POST a click via AJAX --> <!-- have a button POST a click via AJAX -->
<button hx-post="/clicked" hx-swap="outerHTML"> <button hx-post="/clicked" hx-swap="outerHTML">
Click Me Click Me

View File

@ -5,7 +5,7 @@
"AJAX", "AJAX",
"HTML" "HTML"
], ],
"version": "1.6.0", "version": "1.6.1",
"homepage": "https://htmx.org/", "homepage": "https://htmx.org/",
"bugs": { "bugs": {
"url": "https://github.com/bigskysoftware/htmx/issues" "url": "https://github.com/bigskysoftware/htmx/issues"

View File

@ -67,7 +67,7 @@ return (function () {
createWebSocket: function(url){ createWebSocket: function(url){
return new WebSocket(url, []); return new WebSocket(url, []);
}, },
version: "1.6.0" version: "1.6.1"
}; };
var VERBS = ['get', 'post', 'put', 'delete', 'patch']; var VERBS = ['get', 'post', 'put', 'delete', 'patch'];
@ -2481,11 +2481,13 @@ return (function () {
// overriding the detail.shouldSwap property // overriding the detail.shouldSwap property
var shouldSwap = xhr.status >= 200 && xhr.status < 400 && xhr.status !== 204; var shouldSwap = xhr.status >= 200 && xhr.status < 400 && xhr.status !== 204;
var serverResponse = xhr.response; var serverResponse = xhr.response;
var beforeSwapDetails = mergeObjects({shouldSwap: shouldSwap, serverResponse:serverResponse}, responseInfo); var isError = xhr.status >= 400;
var beforeSwapDetails = mergeObjects({shouldSwap: shouldSwap, serverResponse:serverResponse, isError:isError}, responseInfo);
if (!triggerEvent(target, 'htmx:beforeSwap', beforeSwapDetails)) return; if (!triggerEvent(target, 'htmx:beforeSwap', beforeSwapDetails)) return;
target = beforeSwapDetails.target; // allow re-targeting target = beforeSwapDetails.target; // allow re-targeting
serverResponse = beforeSwapDetails.serverResponse; // allow updating content serverResponse = beforeSwapDetails.serverResponse; // allow updating content
isError = beforeSwapDetails.isError; // allow updating error
if (beforeSwapDetails.shouldSwap) { if (beforeSwapDetails.shouldSwap) {
if (xhr.status === 286) { if (xhr.status === 286) {
@ -2600,7 +2602,8 @@ return (function () {
} else { } else {
doSwap(); doSwap();
} }
} else { }
if (isError) {
triggerErrorEvent(elt, 'htmx:responseError', mergeObjects({error: "Response Status Error Code " + xhr.status + " from " + responseInfo.pathInfo.path}, responseInfo)); triggerErrorEvent(elt, 'htmx:responseError', mergeObjects({error: "Response Status Error Code " + xhr.status + " from " + responseInfo.pathInfo.path}, responseInfo));
} }
} }

View File

@ -871,7 +871,7 @@ describe("Core htmx AJAX Tests", function(){
btn.click(); btn.click();
this.server.respond(); this.server.respond();
btn.innerText.should.equal("Clicked!"); btn.innerText.should.equal("Clicked!");
htmx.off("htmx:shouldSwap", handler); htmx.off("htmx:beforeSwap", handler);
}); });
it('400 content can be retargeted if configured to do so', function() it('400 content can be retargeted if configured to do so', function()
@ -891,7 +891,29 @@ describe("Core htmx AJAX Tests", function(){
btn.click(); btn.click();
this.server.respond(); this.server.respond();
div.innerText.should.equal("Clicked!"); div.innerText.should.equal("Clicked!");
htmx.off("htmx:shouldSwap", handler); htmx.off("htmx:beforeSwap", handler);
});
it('errors are triggered only on 400+', function()
{
var errors = 0;
var handler = htmx.on("htmx:responseError", function(){
errors++;
})
this.server.respondWith("GET", "/test1", function (xhr) {
xhr.respond(204, {}, "Clicked!");
});
this.server.respondWith("GET", "/test2", function (xhr) {
xhr.respond(400, {}, "Clicked!");
});
var btn1 = make('<button hx-get="/test1">Click Me!</button>')
var btn2 = make('<button hx-get="/test2">Click Me!</button>')
btn1.click();
btn2.click();
this.server.respond();
this.server.respond();
errors.should.equal(1);
htmx.off("htmx:responseError", handler);
}); });
@ -908,11 +930,10 @@ describe("Core htmx AJAX Tests", function(){
xhr.respond(400, {}, "Clicked!"); xhr.respond(400, {}, "Clicked!");
}); });
var btn = make('<button hx-get="/test">Click Me!</button>') var btn = make('<button hx-get="/test">Click Me!</button>')
var div = make('<div id="d1"></div>')
btn.click(); btn.click();
this.server.respond(); this.server.respond();
div.innerText.should.equal("Clicked!!!"); btn.innerText.should.equal("Clicked!!!");
htmx.off("htmx:shouldSwap", handler); htmx.off("htmx:beforeSwap", handler);
}); });
it('scripts w/ src attribute are properly loaded', function(done) it('scripts w/ src attribute are properly loaded', function(done)

View File

@ -103,13 +103,13 @@ It can be used via [NPM](https://www.npmjs.com/) as "`htmx.org`" or downloaded o
[unpkg](https://unpkg.com/browse/htmx.org/) or your other favorite NPM-based CDN: [unpkg](https://unpkg.com/browse/htmx.org/) or your other favorite NPM-based CDN:
``` html ``` html
<script src="https://unpkg.com/htmx.org@1.6.0"></script> <script src="https://unpkg.com/htmx.org@1.6.1"></script>
``` ```
For added security, you can load the script using [Subresource Integrity (SRI)](https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity). For added security, you can load the script using [Subresource Integrity (SRI)](https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity).
``` html ``` html
<script src="https://unpkg.com/htmx.org@1.6.0" integrity="sha384-G4dtlRlMBrk5fEiRXDsLjriPo8Qk5ZeHVVxS8KhX6D7I9XXJlNqbdvRlp9/glk5D" crossorigin="anonymous"></script> <script src="https://unpkg.com/htmx.org@1.6.1" integrity="sha384-G4dtlRlMBrk5fEiRXDsLjriPo8Qk5ZeHVVxS8KhX6D7I9XXJlNqbdvRlp9/glk5D" crossorigin="anonymous"></script>
``` ```
## <a name="ajax"></a> [AJAX](#ajax) ## <a name="ajax"></a> [AJAX](#ajax)

View File

@ -61,8 +61,8 @@ If you wish to put a template into another file, you can use a directive such as
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width"> <meta name="viewport" content="width=device-width">
<title>JS Bin</title> <title>JS Bin</title>
<script src="https://unpkg.com/htmx.org@1.6.0"></script> <script src="https://unpkg.com/htmx.org@1.6.1"></script>
<script src="https://unpkg.com/htmx.org@1.6.0/dist/ext/client-side-templates.js"></script> <script src="https://unpkg.com/htmx.org@1.6.1/dist/ext/client-side-templates.js"></script>
<script src="https://unpkg.com/mustache@latest"></script> <script src="https://unpkg.com/mustache@latest"></script>
</head> </head>
<body> <body>

View File

@ -35,7 +35,7 @@ By removing these arbitrary constraints htmx completes HTML as a
```html ```html
<!-- Load from unpkg --> <!-- Load from unpkg -->
<script src="https://unpkg.com/htmx.org@1.6.0"></script> <script src="https://unpkg.com/htmx.org@1.6.1"></script>
<!-- have a button POST a click via AJAX --> <!-- have a button POST a click via AJAX -->
<button hx-post="/clicked" hx-swap="outerHTML"> <button hx-post="/clicked" hx-swap="outerHTML">
Click Me Click Me