Search code examples
scala.jsupickle

Minimal example for autowire and upickle


I am not quite getting how the minimal example can be applied to my web app.

In the client code I override the doCall method:

override def doCall(req: Request) = {
   println(req)
   MyServer.routes.apply(req)
}

But how can the client know about MyServer?

Of course, here it works since all code is in the same file, but in reality the client and server communicate via the shared crossproject.

Do I need to write the doCall explicitly with dom.ext.Ajax.post?


Solution

  • I believe what you want is the longer example. The minimal example demonstrates the Autowire API within the same project (not extremely useful). The longer example demonstrates a true client/server interaction.

    For example, it shows how doCall can be implemented with an AJAX call:

    object Client extends autowire.Client[Js.Value, Reader, Writer]{
      override def doCall(req: Request): Future[Js.Value] = {
        dom.ext.Ajax.post(
          url = "/api/" + req.path.mkString("/"),
          data = upickle.json.write(Js.Obj(req.args.toSeq:_*))
        ).map(_.responseText)
         .map(upickle.json.read)
      }
    
      def read[Result: Reader](p: Js.Value) = readJs[Result](p)
      def write[Result: Writer](r: Result) = writeJs(r)
    }