简体   繁体   中英

How to use pattern matching in Binding.scala?

I have the following model:

case class CarBinding(ownerId: Var[String], specs: Var[Option[Specs]])

Specs is a trait and has the following concrete types:

trait Specs {

    def name: String

}

case class SportsCarSpecs(name: String, details: Details) extends Specs

In my Scala.js app, I now want to create a table and list all the entries:

@dom
  def buildTable(): Binding[BindingSeq[Node]] = {
    val data = Vars.empty[CarBinding]
    /* Initial population. */
    // Some code...
      <br/>
        <table class="table table-bordered table-hover">
          <thead>
            <tr>
              <th class="col-md-1">
                <small>Owner ID</small>
              </th>
              <th class="col-md-1">
                <small>Specs</small>
              </th>
            </tr>
          </thead>
          <tbody>
            {for (entry <- data) yield {
            <tr>
              <td>
                <small>
                  {entry.ownerId.bind}
                </small>
              </td>
              <td>
                <small>
                  {entry.specs.bind match {
                  case Some(SportsCarSpecs(name, details)) => {name} <span>{details.ps}</span>
                  case _ => -
                }}
                </small>
              </td>
            </tr>
          }}
          </tbody>
        </table>
      </div>
  }

However, I get the following error:

 ';' expected but $XMLSTART$< found.
[error]                   case Some(SportsCarSpecs(name, details)) => {name} <span>{details.ps}</span>

What am I doing wrong?

This:

entry.specs.bind match {
  case Some(SportsCarSpecs(name, details)) => {name} <span>{details.ps}</span>
  case _ => -
}

isn't a valid expression, so you can't interpolate it in the XML literal. If you had complete XML expressions in both branches, it should work. So the easiest fix I can see is to pull <small> inside:

          <td>
            {entry.specs.bind match {
              case Some(SportsCarSpecs(name, details)) => <small>{name} <span>{details.ps}</span></small>
              case _ => <small>-</small>
            }}
          </td>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM