-
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.
- Loading branch information
Showing
5 changed files
with
65 additions
and
8 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
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,53 @@ | ||
import Foundation | ||
import HTTPTypesHtmx | ||
import Hummingbird | ||
|
||
#if canImport(FoundationNetworking) | ||
import FoundationNetworking | ||
#endif | ||
|
||
extension Route { | ||
static let hxTrigger = Route(.get, "/examples/hx-trigger") | ||
static let hxTriggerAlert = Route(.post, "/examples/hx-trigger") | ||
} | ||
|
||
@Sendable func hxTriggerHandler(_ req: Request) -> Response { | ||
.html( | ||
htmlLayout( | ||
""" | ||
<div> | ||
<h2>HX-Trigger Example</h2> | ||
<p>This example uses HX-Trigger to trigger a client-side event that shows an alert</p> | ||
<div> | ||
<button | ||
hx-post="\(Route.hxTriggerAlert.path)" | ||
hx-swap="none" | ||
>Trigger</button> | ||
</div> | ||
<script> | ||
document.body.addEventListener("showMessage", function(evt){ | ||
alert(evt.detail.value); | ||
}) | ||
</script> | ||
</div> | ||
""" | ||
) | ||
) | ||
} | ||
|
||
@Sendable func hxTriggerAlertHandler(_ req: Request) -> Response { | ||
guard req.isHtmxRequest else { | ||
return Response(status: .badRequest) | ||
} | ||
|
||
return Response( | ||
status: .ok, | ||
headers: HTTPFields([ | ||
.hxTrigger( | ||
event: """ | ||
{"showMessage":"This alert was triggered by HX-Trigger"} | ||
""" | ||
) | ||
]) | ||
) | ||
} |
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