我想在某些网址中添加“卫士”。仅在用户登录时才可以访问这些URL。如果未登录,我想将用户重定向到登录页面。成功登录后,我希望将用户“重定向”到原始哈希。
这是我的路由器
@dom
def outlet: Binding[Node] = {
<div>{
route.state.bind match {
case s: View if UserService.hasRole("admin") => s.render.bind
case s if !UserService.isLoggedIn() => loginPage.render(Some(s.hash)).bind
case _ => <div>Unknown</div>
}
}</div>
loginPage
然后在成功登录后更改窗口哈希:
def login(username: String, password:String, orginalHash: Option[String]) =
UserService.login(username,password).andThen{
case Success(v) =>
println(s"changing hash to $orginalHash")
orginalHash.map(_.substring(1)).foreach(window.location.hash=_)
case Failure(e) => message.value = "Wrong username/password"
}
但是我猜因为哈希实际上并没有改变,所以它不会更新页面:
/index.html/#/foobar
/index.html/#/foobar
)orginalHash
的值为#/foobar
location.hash
更改为/foobar
没有任何作用有没有一种方法可以操纵路由器。也许有一个api可以更改当前页面吗?
答案 0 :(得分:3)
您正在创建一个数据绑定应用程序,但是某些可变状态在您的系统中是不可绑定的。
您应该使UserService.isLoggedIn
和UserService.hasRole("admin")
具有可绑定性。
答案 1 :(得分:2)
感谢@ yang-bo的指导,我通过在val user: Var[Option[User]]
内使用UserService
并将模式匹配更改为
@dom
def outlet: Binding[Node] = {
<div>{
(route.state.bind, UserService.user.bind) match {
case (s: View, Some(_)) => s.render.bind
case (s, None) => loginPage.render(Some(s.hash)).bind
case (_,_) => <div>Unknown</div>
}
}</div>
}