How to read a resource file in Scala.js?

后端 未结 1 1880
后悔当初
后悔当初 2021-01-19 14:18

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,valu         


        
相关标签:
1条回答
  • 2021-01-19 14:56

    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.

    0 讨论(0)
提交回复
热议问题