Question

I want to write Scala code that can be then translated to EmberJS code.

Can it be done? If not out of the box any suggestion as to how that can be achieved by hacking ScalaJS?

Regards.

Was it helpful?

Solution

Scala.js can emit any kind of JavaScript code, so technically the answer is yes. However, since Ember requires you to define "components" using classes of sorts of its own design, it can be a bit ugly to write in Scala.js. For example, this example taken from the front page:

App.GravatarImageComponent = Ember.Component.extend({
  size: 200,
  email: '',

  gravatarUrl: function() {
    var email = this.get('email'),
        size = this.get('size');

    return 'http://www.gravatar.com/avatar/' + hex_md5(email) + '?s=' + size;
  }.property('email', 'size')
});

would have to be written in Scala.js as:

import scala.scalajs.js
import js.Dynamic.{global => g, literal => lit}

g.App.GravatarImageComponent = g.Ember.Component.extend(lit(
  size = 200,
  email = "",

  gravatarUrl = ({ (ths: js.Dynamic) =>
    val email = ths.get("email")
    val size = ths.get("size")
    s"http://www.gravatar.com/avatar/${g.hex_md5(email)}?s=$size"
  }: js.ThisFunction).asInstanceOf[js.Dynamic].property("email", "size")
))

which is, well ... JavaScript-ish.

Powerful UI libraries for JavaScript kind of rely so much on the dynamic and weird aspects of JavaScript that they don't fit super well in Scala.js. I am planning to write a React.js-like UI library specifically designed for Scala.js this semester to address this issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top