Scala.js | Use JavaScript object as function parameter

MayoMayn

BestDev
Oct 18, 2016
1,423
683
So basically I'm exporting a top level object in Scala.js where the start method takes in a JS object as function argument.
After some I discovered that defining a trait for the object would solve the issue, but all I get is still "[Object object]"

This is my main.scala file:
PHP:
package stormengine

import scala.scalajs.js.annotation._
import org.scalajs.dom

import scala.scalajs.js

@js.native
trait Config extends js.Object {
  val host: js.UndefOr[String] = js.native
}

@JSExportTopLevel("StormEngine")
object StormEngine {

  @JSExport
  def start(config: Config): Unit = {
    println(config)
  }
}

Then from my index.html I call the method:
PHP:
<!DOCTYPE html>
<html>
<head>
    <title>Example Scala.js application</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <script type="text/javascript" src="http://localhost:12345/workbench.js"></script>
</head>
<body style="margin: 0px">

<div id="game" />

<script type="text/javascript" src="../storm-engine-fastopt.js"></script>

<script>
    StormEngine.start({
        host: '127.0.0.1'
    });
</script>
</body>
</html>

Any clue how to solve this?
 
My bad it actually did work.
You just cant println the JS object and see the object structure, but when I logged "config.host" instead it did show "127.0.0.1"
 

CosmoPeak

PeakRP.com
May 15, 2016
271
268
Try printing config.host? Or if you want to print the object, you could try JSON.stringify'ing it or overriding the toString() method (if that's supported? Haven't looked at scala.js before)
 

MayoMayn

BestDev
Oct 18, 2016
1,423
683
Try printing config.host? Or if you want to print the object, you could try JSON.stringify'ing it or overriding the toString() method (if that's supported? Haven't looked at scala.js before)
I've made an edit to the post explaining how I solved it, and you were right aswell lol

Sent from my SM-G955F using Tapatalk
 

Users who are viewing this thread

Top