0

The number of parameters in my case class for the data field changes dynamically.

So, can a Var also bind a js.Array in Binding.scala?

I tried following code without success:

case class Data(d: Var[js.Array[String]])
val data = Vars.empty[Data]

{
  for (x <- data) yield {
    val y: js.Array[String] = x.d.bind
    y.zipWithIndex.foreach{case (v, i) => <th>{ y(i) }</th>}
  }
}
user1091344
  • 612
  • 6
  • 27

2 Answers2

2

I could bring it to work on ScalaFiddle.

In essence you have to put each array or option in a Constants object, like:

  @dom
  def render = {
      val bindData= data.bind
      <div>
        {Constants(bindData: _*)
           .map(a => dataElem(a))
           .map(_.bind)}
      </div>
    }

It should have:

  1. Bind the data: val bindData= data.bind
  2. Put Element around: <div> Put
  3. Array in Constants constructor: Constants(bindData: _*)
  4. Call Element with each data-element: .map(a => dataElem(a))
  5. Bind each result: .map(_.bind)

So you go down level by level.

pme
  • 14,156
  • 3
  • 52
  • 95
1

Coming from Binding.scala: Strategy to avoid too many dom-tree updates and have to mention that binding directly on Vars is slow compared to for comprehension.

You should always use for comprehension on Vars, except you are doing some aggregation that must work on the whole list.

For your example, I've created an updated ScalaFiddle: https://scalafiddle.io/sf/7lCiigL/0

Jiaming Lu
  • 875
  • 6
  • 20