为什么不重新评估Binding.scala路由器? [英] Why is the Binding.scala router not reevaluated?

查看:125
本文介绍了为什么不重新评估Binding.scala路由器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to build a generic router for a personal project by Binding.scala.

我已经定义了 PageState 性状

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

  def render: Binding[Node]
}

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

with a number of subclasses for each route type. I've then trying to create a router, which based on the hash chooses the correct class.

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()

}

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

And then I'm my application classes im trying to use this

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 ... 表达式。

I've would expect that changing the hash would force a reevaluation of route.state.bind match ... expression.

任何想法为何这行不通?

Any idea to why this doesn't work?

最诚挚的问候

推荐答案

route.state 仅在不适用返回 Some(newState) newState 不等于先前的状态。

route.state is only changed when unapply returns a Some(newState) and newState does not equal to the previous state.

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

In your case, unapply always returns Some(defaultState) or None. That's why route.state never changes.

这篇关于为什么不重新评估Binding.scala路由器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆