From aaea4afbf3b47d623f396cb1eae247fa92053032 Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Sun, 9 Nov 2014 23:11:06 -0800 Subject: . --- .../scalatex/book/handson/ClientServer.scalatex | 132 +++++++-------------- .../book/handson/PublishingModules.scalatex | 4 +- 2 files changed, 46 insertions(+), 90 deletions(-) (limited to 'book') diff --git a/book/src/main/scalatex/book/handson/ClientServer.scalatex b/book/src/main/scalatex/book/handson/ClientServer.scalatex index b936482..b29df03 100644 --- a/book/src/main/scalatex/book/handson/ClientServer.scalatex +++ b/book/src/main/scalatex/book/handson/ClientServer.scalatex @@ -13,129 +13,85 @@ @p Scala.js lets you share code between client and server relatively straightforwardly. As we saw in the previous chapter, where we made a shared module. Let's work to turn that shared module into a working client-server application! -@sect{Client-Server Configuration} +@sect{A Client-Server Setup} @p Getting started with client-server integration, let's go with the simplest configuration possible: a Spray server and a Scala.js client. Most of the other web-frameworks (Play, Scalatra, etc.) will have more complex configurations, but the basic mechanism of wiring up Scala.js to your web framework will be the same. @p - First, let's do the wiring in @code{build.sbt} to add Spray as a dependency, + First, let's do the wiring in @code{build.sbt}: @hl.ref("examples/crossBuilds/clientserver/build.sbt") - - @p - This does the standard boilerplate for including dependencies via SBT, and does on additional thing: we add the output of @code{fastOptJS} from the client to the @code{resources} on the server. @p - Next, let's kick off the Spray server in our Scala-JVM main method: - - @hl.ref("examples/crossBuilds/clientserver/jvm/src/main/scala/simple/Platform.scala") - + We have two projects: @code{client} and @code{server}, one of which is a Scala.js project (indicated by the presence of @hl.scala{scalaJSSettings}). Both projects share a number of settings: the presence of the @code{shared/} folder, which shared code can live in (similar to what we saw in Cross-platform Modules) and the settings to add Scalatags and uPickle to the build. Note that those two dependencies use the triple @code{%%%} instead of the double @code{%%} to declare: this means that for each dependency, we will pull in the Scala-JVM or Scala.js version depending on whether it's being used in a Scala.js project. @p - This is a not-very-interesting @a("spray-routing", href:="http://spray.io/documentation/1.2.2/spray-routing/") application: we set up a server on @code{localhost:8080}, have the root URL serve a HTML string, and have other URLs serve resources. This includes the @code{js-fastopt.js} file that is now in our resources because of our @code{build.sbt} config earlier! We also add a POST route to allow the client ask the server to format dates any time it wants via Ajax. - + The @code{client} subproject is uneventful, with a dependency on the by-now-familiar @code{scalajs-dom} library. The @code{server} project, on the other hand, is interesting: it contains the dependencies required for us to set up out Spray server, and one additioanl thing: we add the output of @code{fastOptJS} from the client to the @code{resources} on the server. This will allow the @code{server} to serve the compiled-javascript from our @code{client} project from its resources. + @p - Lastly, we'll set up the Scala.js main method, which we are calling in the @hl.html{