Florian H.
Florian H.

Reputation: 304

shared domain with scala.js, how?

Probably a basic question, but I'm confused with the various documentations and examples around scala.js.

I have a domain model I would like to share between scala and scala.js, let's say:

class Estimator(val nickname: String)

... and of course I would like to send objects between the web-client (scala.js with angular via angulate) and the server (scala with spring-mvc on spring-boot).

Should the class extends js.Object? And be annotated with @ScalaJSDefined (not yet deprecated in v0.6.15)?

EDIT 2017-03-30:

Actually this relates to Angulate, the facade for AngularJS I choose. For 2 features (communications to an http server and displaying model fields in html), the domain classes have to be Javascript classes. In Angulate's example, the domain model is duplicated.

There are also (and sadly) no plan to include js.Object in scalajs-stubs to overcome this problem. Details in https://github.com/scala-js/scala-js/issues/2564 . Perhaps js.Object doesn't hurt so much on the jvm...

So, what web frameworks and facade for scala.js does / doesn't nicely support shared domain? Not angulate1, probably Udash, perhaps react?

Upvotes: 0

Views: 214

Answers (1)

Justin du Coeur
Justin du Coeur

Reputation: 2659

(Caveat: I don't know Angulate, which might affect some of this. Speaking generally, though...)

No, those shared objects shouldn't derive from js.Object or use @ScalaJSDefined -- those are only for objects that are designed to interface with JavaScript itself, and it doesn't sound like that's what you have in mind. Objects that are just for Scala don't need them.

But yes -- in general, you're usually going to need to pickle the communications in one way or another. Which pickling library you use is up to you (there are several), but remember that the communication is simply a stream of bytes -- you have to tell the system how to serialize and deserialize between your domain objects and those bytes.

There isn't anything automatic in Scala.js per se -- that's just a language, and doesn't dictate your library choices. You can use implicits to make the pickling semi-automatic, but I recommend being a bit careful with that. I don't see anything obvious in the Angulate docs that indicate that it does the pickling automatically.

Upvotes: 1

Related Questions