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>}
  }
}
2
  • Why not simply use Vars[String]? Commented Mar 22, 2018 at 4:48
  • In fact, the internal cache in a Vars[String] is a js.Array for the JS backend. Commented Mar 22, 2018 at 4:51

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.