scala.js

How can I read a text file with scala.js?

半城伤御伤魂 提交于 2019-12-23 00:28:01
问题 Basically I'm trying to figure out what I need to pass to the onload() method def selectedFile(e: ReactEventI) = { val reader = new dom.FileReader() reader.readAsText(e.currentTarget.files.item(0)) reader.onload( ) } 回答1: You can assign a lambda to the onload handler: reader.onload = (e: UIEvent) => { // Cast is OK, since we are calling readAsText val contents = reader.result.asInstanceOf[String] println(contents) } 回答2: Idiomatic Scala, with error handling Instead of using callbacks, I

sbt play cross build project setup: uTest runner doesn't seperate client/server projects correctly

余生长醉 提交于 2019-12-22 17:35:11
问题 I'm using an build.sbt which has cross compile settings and basically is an adapted version of "Play with scala-js example" and having some trouble getting a clean setup for my tests. Specifically, when running my server tests, my client tests also get executed (which is something that I want to avoid). I followed the instructions from Cannot get uTest to see my tests and added libraryDependencies += "com.lihaoyi" %%% "utest" % "0.3.0" My tests for some reason didn't get executed until I

sbt play cross build project setup: uTest runner doesn't seperate client/server projects correctly

☆樱花仙子☆ 提交于 2019-12-22 17:34:08
问题 I'm using an build.sbt which has cross compile settings and basically is an adapted version of "Play with scala-js example" and having some trouble getting a clean setup for my tests. Specifically, when running my server tests, my client tests also get executed (which is something that I want to avoid). I followed the instructions from Cannot get uTest to see my tests and added libraryDependencies += "com.lihaoyi" %%% "utest" % "0.3.0" My tests for some reason didn't get executed until I

scala.js — getting complex objects from JavaScript

浪子不回头ぞ 提交于 2019-12-21 12:54:08
问题 I'm trying scala.js and I must say that it's completely impressed! However, I try to introduce it into our production little-by-little, working side-by-side with existing JavaScript code. One thing I'm struggling with is passing complex structures from JS to Scala. For example, I have ready-made JS object that I've got from the other JS module: h = { "someInt": 123, "someStr": "hello", "someArray": [ {"name": "a book", "price": 123}, {"name": "a newspaper", "price": 456} ], "someMap": {

How to read a resource file in Scala.js?

穿精又带淫゛_ 提交于 2019-12-19 20:36:21
问题 Let's say I have a map.csv file at the same level (or some other webapp-accessible place) as index-{dev,opt}.html , e.g.: key1,value1 key2,value2 ... keyN,valueN I want to read in that CSV file and end up with a Map[String,String] . I know how to do that in Scala, but how would I do that in Scala.js? I'm trying not to hard-code the keys and values. 回答1: The parsing is basically identical to how you would do it in conventional Scala -- it's the same language, after all. So the real issue is

How to embed javascript code directly in scala.js?

这一生的挚爱 提交于 2019-12-18 15:17:10
问题 When I use scala.js to write scala code to generate javascript, I found it difficult sometimes to wrap the 3rd-party javascript code into scala objects. Is there anyway to embed some javascript code directly? e.g. as strings? 回答1: No, there isn't, on purpose. How would identifiers in the "embedded" JavaScript code bind to identifiers in the surrounding Scala.js code, for example? That would never make any sense. The ugly workaround: js.eval You can "embed" JavaScript code as a parameter to

How do I create “options objects” in Scala.Js?

隐身守侯 提交于 2019-12-17 19:22:27
问题 In idiomatic JavaScript it's common to have a function accept an "options object" as the last parameter. This is where you'd usually put all the option/seldom-used parameters, e.g. jQuery.ajax({ url: "http://www.example.com/foo", success: function() { .. } }) The current documentation for Scala.JS recommands using a Scala trait to represent the options object, but that leads to a problem when you have to create the options since you cannot pass an anonymous class into JavaScript code. How can

How to suppress intellij IDEA error in editor when using Binding.scala macro annotation?

坚强是说给别人听的谎言 提交于 2019-12-14 02:08:24
问题 Despite it compiles and runs in sbt console. Intellij complains that I should have Binding[Node] instead of Elem in editor. @dom def renderDiv: Binding[Div] = <div>...</div> From intellij IDEA's perspective, this method returns a Elem which is a subtype of scala.xml.Node , but when rendering: dom.render(document.getElementById("root"),renderDiv) it requires a org.scalajs.dom.raw.Node . Is there any workaround to this? 回答1: Could put an implicit conversion def in scope: package object xxx {

How to use JSImport when writing scalajs facade for javascript modules

谁都会走 提交于 2019-12-12 23:12:31
问题 I have written a facade using JSImport, and it works. Unfortunately, I arrived at the solution through trial and error, and I don't fully understand why this particular solution works but others I tried did not. Background: I'm starting with a working project, built with sbt, which is a single page application that implements the client side code with scala.js and the server side with scala and the Play framework. The javascript libraries were packaged with web jars and bundled into the

Toomany DOM updates

北城余情 提交于 2019-12-12 19:20:30
问题 The link [https://ccamel.github.io/playground-binding.scala/index.html#playground-binding.scala/home] has few demos of binding.scala I have used DomListner extension in chrome to understand the dom events. I found for each interaction there are hundreds of DOM events fired. For example one click on calculator button results in 114 events. It this a performance issue ? Does binding.scala library need performance improvements ? Does the code written using binding.scala need optimization ? 回答1: