【问题标题】:How to create a javascript function using Scala.js?如何使用 Scala.js 创建一个 javascript 函数?
【发布时间】: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 {内,这是创建svgContainertestFunction的正确方法吗?

【问题讨论】:

    标签: javascript scala d3.js scala.js


    【解决方案1】:

    在 Scala.js 中,scala.FunctionNs 可以隐式转换为 js.FunctionNs 并返回,因此您基本上不需要做任何事情:只需将 lambda 传递给 JavaScript 调用。在“在 Scala.js 中设置 UI”下有一个 in Step 5 of the the tutorial 的示例。对于您的代码,它看起来像这样:

    jQuery(dom.document).ready { () =>
      val svgContainer = ...
    }
    

    您可以在calling JavaScript guide 中找到更多信息。

    更新:

    这是你整个 JavaScript sn-p 的翻译:

    import scala.scalajs.js
    import org.scalajs.dom           // see https://github.com/scala-js/scala-js-dom
    import org.scalajs.jquery.jQuery // see https://github.com/scala-js/scala-js-jquery
    
    object ScalaJSExample extends js.JSApp {
      val d3 = js.Dynamic.global.d3 // assuming you don't have static types for d3, here
    
      def main(): Unit = {
        jQuery(dom.document).ready { () =>
          val svgContainer =
            d3.select("body").append("svg")
              .attr("width", 1200)
              .attr("height", 1200)
              .attr("text-align", "center")
          testFunction(svgContainer)
        }
      }
    
      def testFunction(svgContainer: js.Dynamic): Unit = {
        dom.alert(svgContainer.toString())
      }
    }
    

    如你所见:

    1. 对于具有 Scala.js 静态类型的库(例如 DOM 和 jQuery),最好使用这些静态类型。这里以静态类型的方式使用 ready()dom.documentdom.alert
    2. 当您没有静态类型时,可以使用 js.Dynamic 以动态类型的方式操作 JavaScript 值,使用正常语法(不是字符串)
    3. 您使用def 定义函数。它们是否被编译为 JavaScript 函数对你来说并不重要:只需编写你的 Scala 代码而不考虑它,编译器就会完成它的工作。

    【讨论】:

    • 请查看问题更新,我不确定如何将 svgContainer 参数传递给函数并调用相同的函数?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    相关资源
    最近更新 更多