Fix JS build and move example to its own module

This commit is contained in:
Paul-Henri Froidmont 2025-08-21 14:10:47 +02:00
parent 73510857a6
commit 486e89c1f1
Signed by: phfroidmont
GPG key ID: BE948AFD7E7873BE
5 changed files with 64 additions and 63 deletions

20
example/src/Example.scala Normal file
View file

@ -0,0 +1,20 @@
import zio.*
import zio.http.*
import scalive.*
object Example extends ZIOAppDefault:
val liveRouter =
LiveRouter(
RootLayout(_),
List(
LiveRoute(
Root,
(_, req) =>
val q = req.queryParam("q").map("Param : " ++ _).getOrElse("No param")
ExampleLiveView(q)
)
)
)
override val run = Server.serve(liveRouter.routes).provide(Server.default)

View file

@ -0,0 +1,36 @@
import scalive.*
final case class ExampleModel(elems: List[NestedModel], cls: String = "text-xs")
final case class NestedModel(name: String, age: Int)
class ExampleLiveView(someParam: String) extends LiveView[Nothing]:
val model = Var(
ExampleModel(
List(
NestedModel("a", 10),
NestedModel("b", 15),
NestedModel("c", 20)
)
)
)
def handleCommand(cmd: Nothing): Unit = ()
val el =
div(
h1(someParam),
idAttr := "42",
cls := model(_.cls),
ul(
model(_.elems).splitByIndex((_, elem) =>
li(
"Nom: ",
elem(_.name),
" Age: ",
elem(_.age.toString)
)
)
)
)
end ExampleLiveView

View file

@ -0,0 +1,13 @@
import scalive.*
object RootLayout:
def apply(content: HtmlElement): HtmlElement =
htmlRootTag(
lang := "en",
headTag(
metaTag(charset := "utf-8")
),
bodyTag(
content
)
)