问题
When I use Binding.scala, I want to create some div
s 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?
回答1:
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:
- Refactoring the code in the closure into its own
@dom
annotated method. Converting
someCollection
to aBindingSeq
, for example:Constants(someCollection: _*).map { item => <div>{item.bind}</div> }
- 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>
来源:https://stackoverflow.com/questions/42498968/when-i-use-binding-scala-i-got-the-error-each-instructions-must-be-inside-a-sd