@chg20 I wrote a sample sse example with a flask server. I make the following change to:
function processSSETrigger(elt, verb, path, sseEventName) {
var sseSourceElt = getClosestMatch(elt, function (parent) {
return getInternalData(parent).sseEventSource != null;
});
if (sseSourceElt) {
var sseEventSource = getInternalData(sseSourceElt).sseEventSource;
var sseListener = function (event) {
if (!maybeCloseSSESource(sseSourceElt)) {
if (bodyContains(elt)) {
getInternalData(elt).sseEvent = event;
issueAjaxRequest(elt, verb, path);
} else {
sseEventSource.removeEventListener(sseEventName, sseListener);
}
}
noticed I assigned the event to internal data of the element, this may not be the best way to do it.
Can you make some recommendations?
795c787,788
< var sseListener = function () {
---
>
> var sseListener = function (event) {
797a791
> getInternalData(elt).sseEvent = event;
htmx.defineExtension('sse-body', {
encodeParameters : function(headers, parameters, elt) {
headers["Content-Type"] = "application/json";
return elt["htmx-internal-data"].sseEvent.data;
}
});
<ol hx-sse="swap on eventName" hx-swap="beforeend">Loading list...</ol>
<body id="thebody" hx-sse="connect /news_updates">
<h1>Receiving Messages:</h1>
<div id="sselist" hx-trigger="sse:new_news" hx-ext="sse-body"
hx-post="/news" hx-target="#nid" hx-swap="beforeend">
<ul id="nid">
</ul>
</div>
</body>
<body id="thebody" hx-sse="connect /news_updates">
<h1>Receiving Messages:</h1>
<ol hx-trigger="sse:new_news" hx-get="/news" hx-swap="beforeend">
loading list ...
</ol>
</body>
<div _="on click add .clickedClass">...</div>
<div hx-trigger="removeMe" hx-delete="/my/url" _"on click add .fadeMe then wait 500ms then trigger removeMe">
Remove Me
</div>