How to read a resource file in Scala.js?

点点圈 提交于 2019-12-01 19:42:40

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 fetching the file. There's no one-size-fits-all solution there; it depends on what you're using as the web server, for example. My own system is Play-based, and the cognate code looks like this:

override def postInit() = {
  val ajaxCall:PlayAjax = controllers.Assets.versioned("messages/default/clientStrings")
  ajaxCall.callAjax().map { messageText =>
    val hoconTable = HoconParse(messageText)
    _messages = Some(MessagesImpl("", hoconTable))
    _readyPromise.complete(Success())
  }
}

The details there are particular to my (rather complex) setup, but the basic principle is straightforward: issue an AJAX call to load the file as text, then parse that file.

There are other options as well -- for example, loading and parsing the file server-side, and sending it to the client as strongly-typed structures using something like Autowire. It all depends on what your infrastructure looks like.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!