我創建使用ScalaJS項目:如何使用Scala.js創建JavaScript函數?
http://www.scala-js.org/doc/tutorial.html
在http://www.scala-js.org/doc/faq.html閱讀的文檔,它似乎並不認爲創建和調用JavaScript功能描述?
如何創建JavaScript函數並調用它?
我將手動頭元素添加維生素D3的index.html:
<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打字,會callFunction
被作爲一個JavaScript函數,在使用內置fastOptJS
創建?
在jQuery(dom.document).ready {
以內,這是創建svgContainer
和testFunction
的正確方法嗎?
請參閱問題更新,我不確定如何將svgContainer參數傳遞給函數並調用相同的函數? – 2014-12-07 14:38:58
@ blue-sky我已經更新了我的答案。 – sjrd 2014-12-07 14:56:41