From bc2c9eb9371894eef1ef94da37001cf0837f621b Mon Sep 17 00:00:00 2001 From: carson Date: Fri, 26 Feb 2021 14:11:45 -0700 Subject: [PATCH 1/2] find the correct element to trigger after request events on --- src/htmx.js | 36 +++++++++++++++++++++++++++++------- test/core/api.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 7 deletions(-) diff --git a/src/htmx.js b/src/htmx.js index 401e9361..11a25cb8 100644 --- a/src/htmx.js +++ b/src/htmx.js @@ -1923,16 +1923,22 @@ return (function () { function ajaxHelper(verb, path, context) { if (context) { if (context instanceof Element || isType(context, 'String')) { - issueAjaxRequest(verb, path, null, null, null, resolveTarget(context)); + return issueAjaxRequest(verb, path, null, null, null, resolveTarget(context)); } else { - issueAjaxRequest(verb, path, resolveTarget(context.source), context.event, context.handler, resolveTarget(context.target)); + return issueAjaxRequest(verb, path, resolveTarget(context.source), context.event, context.handler, resolveTarget(context.target)); } } else { - issueAjaxRequest(verb, path); + return issueAjaxRequest(verb, path); } } function issueAjaxRequest(verb, path, elt, event, responseHandler, targetOverride) { + var resolve = null; + var reject = null; + var promise = new Promise(function (_resolve, _reject) { + resolve = _resolve; + reject = _reject; + }); if(elt == null) { elt = getDocument().body; } @@ -1970,12 +1976,18 @@ return (function () { // prompt returns null if cancelled and empty string if accepted with no entry if (promptResponse === null || !triggerEvent(elt, 'htmx:prompt', {prompt: promptResponse, target:target})) - return endRequestLock(); + resolve(); + endRequestLock(); + return promise; } var confirmQuestion = getClosestAttributeValue(elt, "hx-confirm"); if (confirmQuestion) { - if(!confirm(confirmQuestion)) return endRequestLock(); + if(!confirm(confirmQuestion)) { + resolve(); + endRequestLock() + return promise; + } } var xhr = new XMLHttpRequest(); @@ -2018,7 +2030,9 @@ return (function () { if(errors && errors.length > 0){ triggerEvent(elt, 'htmx:validation:halted', requestConfig) - return endRequestLock(); + resolve(); + endRequestLock(); + return promise; } var splitPath = path.split("#"); @@ -2071,6 +2085,7 @@ return (function () { } triggerEvent(finalElt, 'htmx:afterRequest', responseInfo); triggerEvent(finalElt, 'htmx:afterOnLoad', responseInfo); + resolve(); endRequestLock(); } } @@ -2082,6 +2097,7 @@ return (function () { } triggerErrorEvent(finalElt, 'htmx:afterRequest', responseInfo); triggerErrorEvent(finalElt, 'htmx:sendError', responseInfo); + reject(); endRequestLock(); } xhr.onabort = function() { @@ -2092,9 +2108,14 @@ return (function () { } triggerErrorEvent(finalElt, 'htmx:afterRequest', responseInfo); triggerErrorEvent(finalElt, 'htmx:sendAbort', responseInfo); + reject(); endRequestLock(); } - if(!triggerEvent(elt, 'htmx:beforeRequest', responseInfo)) return endRequestLock(); + if(!triggerEvent(elt, 'htmx:beforeRequest', responseInfo)){ + resolve(); + endRequestLock() + return promise + } var indicators = addRequestIndicatorClasses(elt); forEach(['loadstart', 'loadend', 'progress', 'abort'], function(eventName) { @@ -2109,6 +2130,7 @@ return (function () { }); }); xhr.send(verb === 'get' ? null : encodeParamsForBody(xhr, elt, filteredParameters)); + return promise; } function handleAjaxResponse(elt, responseInfo) { diff --git a/test/core/api.js b/test/core/api.js index 856a7ac1..1d1a7e7a 100644 --- a/test/core/api.js +++ b/test/core/api.js @@ -261,5 +261,36 @@ describe("Core htmx API test", function(){ calledEvent.should.equal(true); }); + it('ajax api works', function() + { + this.server.respondWith("GET", "/test", "foo!"); + var div = make("
"); + htmx.ajax("GET", "/test", div) + this.server.respond(); + div.innerHTML.should.equal("foo!"); + }); + + it('ajax api works by ID', function() + { + this.server.respondWith("GET", "/test", "foo!"); + var div = make("
"); + htmx.ajax("GET", "/test", "#d1") + this.server.respond(); + div.innerHTML.should.equal("foo!"); + }); + + it('ajax returns a promise', function(done) + { + this.server.respondWith("GET", "/test", "foo!"); + var div = make("
"); + var promise = htmx.ajax("GET", "/test", "#d1"); + this.server.respond(); + div.innerHTML.should.equal("foo!"); + promise.then(function(){ + done(); + }) + }); + + }) From 53c195c5ca4f0d5089e1f09aba9aba51286562fe Mon Sep 17 00:00:00 2001 From: carson Date: Fri, 26 Feb 2021 14:12:04 -0700 Subject: [PATCH 2/2] bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a086fd05..7155cf93 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "AJAX", "HTML" ], - "version": "1.2.1", + "version": "1.2.2", "homepage": "https://htmx.org/", "bugs": { "url": "https://github.com/bigskysoftware/htmx/issues"