I've created a ScalaJS project using:
http://www.scala-js.org/doc/tutorial.html
Reading the docs at http://www.scala-js.org/doc/faq.html, it does not seem that creating and calling a JavaScript function is described?
How do I create a JavaScript function and invoke it?
I'll manually add d3 to the head element to index.html:
<head>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
</head>
But how do I create the below code using ScalaJS?
$(document).ready(function () {
var svgContainer = d3.select("body").append("svg")
.attr("width", 1200)
.attr("height", 1200)
.attr("text-align", "center");
testFunction(svgContainer);
});
<script>
function testFunction(svgContainer) {
alert(svgContainer)
}
</script>
Entire index.html:
<!DOCTYPE html>
<html>
<head>
<title>Example Scala.js application</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h1>Example Scala.js application - full-optimized version</h1>
<p>After having compiled and full-optimized properly the code for the application
(using `sbt fullOptJS`), you should see "It works" herebelow.
See README.md for detailed explanations.</p>
<div id="playground">
</div>
<script type="text/javascript" src="./target/scala-2.10/my-project-fastopt.js"></script>
<script type="text/javascript" src="./target/scala-2.10/my-project-launcher.js"></script>
</body>
</html>
Update:
object ScalaJSExample extends js.JSApp {
def main(): Unit = {
jQuery(dom.document).ready { () => {
val svgContainer = "d3.select(\"body\").append(\"svg\").attr(\"width\", 1200).attr(\"height\", 1200).attr(\"text-align\", \"center\")";
val function = "callFunction(svgContainer)";
}
}
}
def callFunction(svgContainer): Unit = {
}
}
In callFunction should svgContainer be typed, will callFunction be created as a JavaScript function when built using fastOptJS?
Within jQuery(dom.document).ready {, is this the correct method of creating svgContainer and testFunction?