4

I'm trying to combine 2 binding fragments into one without recourse to an englobing XML literal.

See the following code:

@dom def a = <div><p>a</p></div>
@dom def b = <div><p>b</p></div>

@dom def block = {
  // <div>
  {a.bind}
  {b.bind}
  // </div>
}

See ScalaFiddle

As expected, this does not work, only b is displayed.

What I'm looking for here is a way to combine 2 fragments into one, through a function with this signature (for example)

combine: Binding[Node] x Binding[Node] -> Binding[BindingSeq[Node]]

How is it possible ?

Thanks :)

Chris
  • 63
  • 4

1 Answers1

4

https://scalafiddle.io/sf/9cLgxbN/1

def block = Binding(Constants(a.bind, b.bind))

or

https://scalafiddle.io/sf/9cLgxbN/6

def block = Binding(Constants(a, b).map(_.bind))

The latter one is able to partially update, while the former one is not.

For BindingSeq:

https://scalafiddle.io/sf/9cLgxbN/7

@dom def a = <div><p>a</p></div><div>b</div>
@dom def b = <div><p>c</p></div><div>d</div>
def block = Binding(Constants((a.bind.all.bind ++ b.bind.all.bind): _*))

or

https://scalafiddle.io/sf/9cLgxbN/8

def block = Binding(Constants(a, b).flatMap(_.bind))
Yang Bo
  • 3,586
  • 3
  • 22
  • 35