【发布时间】:2015-02-05 05:06:35
【问题描述】:
我使用以下方法创建了一个 ScalaJS 项目:
http://www.scala-js.org/doc/tutorial.html
阅读http://www.scala-js.org/doc/faq.html 的文档,似乎没有描述创建和调用 JavaScript 函数?
如何创建 JavaScript 函数并调用它?
我会手动将 d3 添加到 index.html 的 head 元素中:
<head>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
</head>
但是如何使用 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>
整个 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>
更新:
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 = {
}
}
在callFunction 中应该键入svgContainer,当使用fastOptJS 构建时,callFunction 是否会被创建为 JavaScript 函数?
在jQuery(dom.document).ready {内,这是创建svgContainer和testFunction的正确方法吗?
【问题讨论】:
标签: javascript scala d3.js scala.js