0

Finding it hard to get good resources online - but simple things like, grabbing some JSON from an api, running a map, filter and some reducers seem very very cumbersome. Seem to be running json.asInstanceOf[js.Array[js.Dynamic]] alot. And then things like .filter(_.width <= 640) complain about type mismatches etc...

Surely there has to be a quicker way to associate types than having to type asInstanceOf over and over again.

Marais Rossouw
  • 937
  • 1
  • 10
  • 29
  • 2
    Are you creating facades? It's usually necessary to build the `asInstanceOf` stuff *once*, to associate with a facade, but then you use that facade afterwards. If you haven't already, I strongly recommend reading [this doc page](https://www.scala-js.org/doc/interoperability/facade-types.html). – Justin du Coeur Aug 25 '16 at 00:36
  • So the "workflow" per say for a ScalaJS app - would be to facade everything? – Marais Rossouw Aug 25 '16 at 01:13
  • Why can't you just go `myJsonArray[0]` i know you need to go `myJsonArray(0)` and do that Bracket annotation, is this needed for any array query, or will the facade fix that? – Marais Rossouw Aug 25 '16 at 01:14
  • Possible duplicate of [Parsing the JSON representation of database rows in Scala.js](http://stackoverflow.com/questions/35363007/parsing-the-json-representation-of-database-rows-in-scala-js) – sjrd Aug 28 '16 at 11:12
  • Why not just use a library like upickle to parse it to a case class and work with it from there? – jbrown Sep 02 '16 at 10:28
  • @jbrown wouldn't that mean id be stuck with 100kb of library, that `JSON.parse()` would give me? – Marais Rossouw Sep 03 '16 at 08:03
  • @MaraisRossouw yes, JSON.parse() will parse it, but upickle will parse it to strongly-typed data structures (maps, case classes, etc). It'll therefore catch lots of errors at compile time, one of the benefits of scalajs IMHO. You can also use upickle server-side too, so could share your data structures using a scalajs cross project. I'm not sure how compact it is though, but IIRC using scalajs anyway adds several 100kb, so may not be right if you need a really small footprint. – jbrown Sep 05 '16 at 10:42

0 Answers0