【问题标题】:Why is the Binding.scala router not reevaluated?为什么没有重新评估 Binding.scala 路由器?
【发布时间】:2018-05-07 04:35:21
【问题描述】:

我正在尝试通过 Binding.scala 为个人项目构建通用路由器。

我已经定义了一个 PageState 特征

sealed trait WhistState {
  def text: String
  def hash: String

  def render: Binding[Node]
}

每种路线类型都有许多子类。然后我尝试创建一个路由器,它根据哈希选择正确的类。

object Router {

  val defaultState: WhistState = DefaultState("Games")

  val allStates: Vector[WhistState] = Vector(defaultState)


  val route: Route.Hash[WhistState] = Route.Hash[WhistState](defaultState)(new Route.Format[WhistState] {

    override def unapply(hashText: String): Option[WhistState] = allStates.find(_.hash == window.location.hash)
    override def apply(state: WhistState): String = state.hash

  })

  route.watch()

}

然后我是我的应用程序类,我正在尝试使用它

object Application {
  import example.route.Router._

  @dom
  def render: Binding[Node] = {
    for (hash <- route.state.bind.hash) yield println("hash: " + hash)

    route.state.bind match {
      case default: WhistState => println("default"); default.render.bind
      case _                   => println("none");    <div>NotFound</div>
    }
  }

  def main(args: Array[String]): Unit = {
    dom.render(document.querySelector("#content"), render)
  }

}

我预计更改哈希会强制重新评估 route.state.bind match ... 表达式。

知道为什么这不起作用吗?

最好的问候

【问题讨论】:

    标签: scala routes scala.js binding.scala


    【解决方案1】:

    route.state 仅在 unapply 返回 Some(newState) 并且 newState 不等于之前的状态时更改。

    在您的情况下,unapply 始终返回 Some(defaultState)None。这就是为什么route.state 永远不会改变的原因。

    【讨论】:

    • @HenrikKirk 你能写一个关于如何使用路线的简单要点(这里是一个新的东西)
    猜你喜欢
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-01
    • 2014-11-12
    • 2018-01-11
    • 1970-01-01
    相关资源
    最近更新 更多