3

When I use Binding.scala, I want to create some divs according to source data someCollection:

val someCollection = Seq("foo", "bar")
someCollection.map { item =>
  <div>{item.bind}</div>
}

However, I got a compiler error each instructions must be inside a SDE block.

How can I fix this?

chengpohi
  • 14,064
  • 1
  • 24
  • 42
Yang Bo
  • 3,586
  • 3
  • 22
  • 35
  • I need someone help to create a `binding.scala` tag for this question. See https://github.com/ThoughtWorksInc/Binding.scala/issues/47 – Yang Bo Feb 28 '17 at 02:24

1 Answers1

6

The code that causes this error is that your bind expression must not be outside of the scope of the @dom macro. This can happen when creating a closure and can be resolved by:

  1. Refactoring the code in the closure into its own @dom annotated method.
  2. Converting someCollection to a BindingSeq, for example:

    Constants(someCollection: _*).map { item => <div>{item.bind}</div> }

  3. Provide a scalaz.Traverse type class for the collection (Run this example on ScalaFiddle)

TL;DR

@dom def renderList(data: List[Binding[String]]) = <ol>{
  import scalaz.std.list._ // Type classes for List
  for (b <- data) yield {
    <li>{b.bind}</li>
  }
}</ol>
Yang Bo
  • 3,586
  • 3
  • 22
  • 35