mirror of
https://github.com/phfroidmont/scalive.git
synced 2025-12-25 05:26:59 +01:00
Fix JS build and move example to its own module
This commit is contained in:
parent
73510857a6
commit
486e89c1f1
5 changed files with 64 additions and 63 deletions
|
|
@ -29,10 +29,10 @@ object core extends ScalaCommon:
|
||||||
}
|
}
|
||||||
|
|
||||||
def jsBundle = Task {
|
def jsBundle = Task {
|
||||||
val bundleDest = Task.dest / "static" / "scalive.js"
|
|
||||||
os.copy(
|
os.copy(
|
||||||
from = js.bundle().path,
|
from = js.bundle().path,
|
||||||
to = bundleDest
|
to = Task.dest / "static" / "scalive.js",
|
||||||
|
createFolders = true
|
||||||
)
|
)
|
||||||
PathRef(Task.dest)
|
PathRef(Task.dest)
|
||||||
}
|
}
|
||||||
|
|
@ -46,4 +46,7 @@ object core extends ScalaCommon:
|
||||||
|
|
||||||
object zio extends ScalaCommon:
|
object zio extends ScalaCommon:
|
||||||
def mvnDeps = Seq(mvn"dev.zio::zio-http:3.4.0")
|
def mvnDeps = Seq(mvn"dev.zio::zio-http:3.4.0")
|
||||||
override def moduleDeps = Seq(core)
|
def moduleDeps = Seq(core)
|
||||||
|
|
||||||
|
object example extends ScalaCommon:
|
||||||
|
def moduleDeps = Seq(zio)
|
||||||
|
|
|
||||||
20
example/src/Example.scala
Normal file
20
example/src/Example.scala
Normal 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)
|
||||||
36
example/src/ExampleLiveView.scala
Normal file
36
example/src/ExampleLiveView.scala
Normal 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
|
||||||
|
|
@ -1,6 +1,4 @@
|
||||||
package scalive
|
import scalive.*
|
||||||
|
|
||||||
import scalive.HtmlElement
|
|
||||||
|
|
||||||
object RootLayout:
|
object RootLayout:
|
||||||
def apply(content: HtmlElement): HtmlElement =
|
def apply(content: HtmlElement): HtmlElement =
|
||||||
|
|
@ -1,56 +0,0 @@
|
||||||
package scalive
|
|
||||||
|
|
||||||
import zio.*
|
|
||||||
import zio.http.*
|
|
||||||
|
|
||||||
object Example extends ZIOAppDefault:
|
|
||||||
|
|
||||||
val liveRouter =
|
|
||||||
LiveRouter(
|
|
||||||
RootLayout(_),
|
|
||||||
List(
|
|
||||||
LiveRoute(
|
|
||||||
Root / "test",
|
|
||||||
(_, req) =>
|
|
||||||
val q = req.queryParam("q").map("Param : " ++ _).getOrElse("No param")
|
|
||||||
TestView(q)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
override val run = Server.serve(liveRouter.routes).provide(Server.default)
|
|
||||||
|
|
||||||
final case class MyModel(elems: List[NestedModel], cls: String = "text-xs")
|
|
||||||
final case class NestedModel(name: String, age: Int)
|
|
||||||
|
|
||||||
class TestView(someParam: String) extends LiveView[Nothing]:
|
|
||||||
|
|
||||||
val model = Var(
|
|
||||||
MyModel(
|
|
||||||
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 TestView
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue