Alex
Alex

Reputation: 9478

Do you know a Scala to Javascript DSL/compiler/something?

Do you know a Scala to Javascript DSL/compiler/something? I want to program my Javascript code with Scala so i dont have to write so much JS code. Thanks for any help!

Upvotes: 5

Views: 1211

Answers (5)

nau
nau

Reputation: 1165

There is JScala - Scala macro that translates Scala code to JavaScript.

Upvotes: 1

nafg
nafg

Reputation: 2534

Reactive-web has a quite-complete, easy-to-read DSL for writing Javascript in Scala. Actually there are two parts to the DSL: expressions and statements.

Statements use a builder DSL (applying objects in the DSL puts an instance on a thread-local stack which can be collected). If you enclose it in a Javascript { ... } block, the javascript will be sent to the browser, in a Lift webapp. If you just want it to return the DSL object, enclose it in val (_, theStatements) = JsStatement.inScope{ ... }. You can render it to a String by passing the result to JsStatement.render.

Here's some code from the test:

  If(true) {
    window.alert("True")
  }.ElseIf (false){
    window.alert("False")
  } Else {
    If(true) {
    } Else {
    }
  }
  While(true) {
    window.alert("Again!")
  }
  Do {
    window.alert("Hello!")
  } While (false)
  Switch(1)(
    0.$ :> {
      window.alert("No")
    },
    1.$ :> window.alert("Yes")
  )
  object i extends JsVar[JsNumber]
  For(List(i := 1), i < 10, List(i := i + 1)) {}

  Page.withPage(new Page){
    for (j <- List(1.$, 2.$, 3.$)$) {
      If(j > 1) {
        window.alert("Greater"$)
      }
    }
    for (j <- Each(List(1.$, 2.$, 3.$))) {
      If(j > 1) {
        window.alert("Greater")
      }
    }
    Try {
      Throw("message")
    } Catch { c =>
    } Finally {
    }
  }

  object myFunc extends Function({ x: $[JsNumber] =>
    If(x > 10) {
      window alert "Greater"
    } Else {
      window alert "Small"
    }
  })
  myFunc(10)
  Page.withPage(new Page) {
    val myFunc2 = Function({ x: $[JsNumber] => Return(x > 10) })

    val myAjax = Ajax{ x: String => println("Got "+x) }
    myAjax("Hello server!")
  }

Expressions

Some code from the tests (note that while it currently uses $, this should be changed in the future to something more readable):

(1.$ + 2 render) should equal (new JsOp(1, 2, "+").render)

{ () =>
  If(true) {
    window alert "Greater"
  } Else {
    window alert "Small"
  }
}.$.render should equal (
  "(function(){if(true) {window.alert(\"Greater\")} else {window.alert(\"Small\")};return })"
)

Upvotes: 2

thoredge
thoredge

Reputation: 12631

You could try out the JsCmds and JqJsCmds (jquery) of Lift to see how far that will support your needs. It works very good in Lift-applications (haven't had to fall back to raw javascript yet).

Upvotes: 6

David
David

Reputation: 5214

There's also the Scala-GWT project.

Upvotes: 7

Gursel Koca
Gursel Koca

Reputation: 21300

Well, what you can do decompile your scala class to java source, then compile decompiled java source with GWT.. Well, but you should know that GWT supports only a subset of JDK..

Upvotes: 2

Related Questions