user79074 user79074 - 1 month ago 11
Scala Question

Dynamic calls to JavaScript in Scala.js

I am wondering how to do dynamic operations in Scala.js. For example, looking at the jQuery example in the tutorial, my understanding is you define the following in scala:

object TutorialApp extends JSApp {
def appendPar(msg: String) = {
jQuery("body").append("<p>" + msg + "</p>")
}
def main(): Unit = {
appendPar("Hello World")
}
}


This is all stuff that is generated statically at compile time. But I didn't see any way I could set the message parameter dynamically (eg read it from a DB).

Answer

I don't know about reading it from the DB. That is beyond the scope of this question (or you need to rephrase the question). Maybe an AJAX call or something?

But to read it from, for example, an <input> tag, you'd do something like that:

def main(): Unit = {
  val msg = jQuery("#myinput").value()
  appendPar(msg)
}

(Although in this case it probably doesn't make any sense in a main method, but that's not the point.)

I mean, msg is just a val (so like a var in JS but immutable). You can fetch it from any dynamic source of information as you like.

Edit:

If you want to access some data generated dynamically by the server when rendering the page, you can do so like this:

First, have your server generate the data as a global var in a <script> tag in the generated HTML. Something like:

<script type="text/javascript">
var mydata = {
  msg: "Some text generated dynamically by the server"
}
</script>

Make sure to emit this script tag before the call to the main() function of Scala.js!

Then, from Scala.js, you can access these data with the js.Dynamic interface:

import scala.scalajs.js

val mydata = js.Dynamic.global.mydata
val msg = mydata.msg.asInstanceOf[String]

If your data have always a relatively static structure, it may be useful to declare yourself a facade type for them:

@JSName("mydata")
object MyData extends js.Object {
  val msg: String = ???
}

Then you can access it without resorting to the Dynamic API:

val msg = MyData.msg
Comments