我正在尝试通过 Binding.scala 为个人项目构建通用路由器。
我定义了一个PageState
trait
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 ...
表达。
知道为什么这不起作用吗?
此致
route.state
仅在以下情况下更改unapply
返回一个Some(newState)
and newState
不等于之前的状态。
就你而言,unapply
总是返回Some(defaultState)
or None
。这就是为什么route.state
永远不会改变。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)