Scala-提升-映射自定义盒装对象以进行绑定? [英] Scala - Lift - map a custom boxed object for bind?
问题描述
好,所以我有一个自定义对象,它是一个myUser.
Ok, so i have a custom object that is a myUser.
myUser看起来像这样:
myUser looks like this :
username:String = ""
firstname:String = ""
lastname:String = ""
我有进入页面的这些用户的列表,我建立了一个链接,以查看每个用户的详细信息.当您单击某个用户时,它将填充我作为请求变量的装箱的对象,然后直接进入详细信息页面以查看此用户信息.为什么我不能从该对象的地图上绑定?这是一些代码...
I have a list of these users that come into a page, I've made a link to view the details of each user. When you click on a user, it will fill the boxed object that I have as a request var, and then direct to the detail page to view this user information. Why is that I can't bind from a map on this object? Here is some code...
private object selectedUser extends RequestVar[Box[myUser]](Empty)
def getusers(html: NodeSeq):NodeSeq = {
//This gets me a list of 10 users that are "myUser" objects
val userList = User.getUsers(10)
userList.flatMap{user => bind("user", html,
"username" -> SHtml.link("/%2Fadmin%2Fdetail", () => selectedUser(Full(user)), Text(user.username)),
"firstname" -> {user.firstname},
"lastname" -> {user.lastname},
"lastloggedin" -> {user.lastloggedin})}
}
现在,当我到达用户详细信息页面时,我想映射出我已经编写的selectedUser对象....但是由于某种原因,我无法使其正常工作,这给了我这个错误:
Now when I arrive at the user detail page, I want to map out the selectedUser object i've written....but for some reason, I can't get it to work, it's giving me this error :
类型不匹配;成立 : net.liftweb.common.Box [scala.xml.NodeSeq] 必需:scala.xml.NodeSeq
type mismatch; found : net.liftweb.common.Box[scala.xml.NodeSeq] required: scala.xml.NodeSeq
这是给我这个错误的代码:
Here is the code that's giving me this error :
def userdetail(html: NodeSeq):NodeSeq = {
selectedUser.is.map{user => bind("user", html,
"username" -> {user.username},
"firstname" -> {user.firstname},
"lastname" -> {user.lastname},
"lastloggedin" -> {user.lastloggedin})}
}
有趣的是,我可以这样做,并且可以正常工作,但是必须有一种方法可以在一行上完成它吗?
The interesting thing is, i can do it this way, and it will work, but there has got to be a way to do it on one line right?
这可行...但是很麻烦:
This works...but it's cumbersome :
def userdetail(html: NodeSeq):NodeSeq = {
var username = ""
var firstname = ""
var lastname = ""
var lastloggedin = ""
bind("user", html,
"username" -> {username},
"firstname" -> {firstname},
"lastname" -> {lastname},
"lastloggedin" -> {lastloggedin})
}
有人可以告诉我我在这里想念的小东西吗?我希望我能足够清楚地解释自己.
Can someone please tell me the little thing i'm missing here? I hope I explained myself clearly enough.
谢谢!
推荐答案
这将为您提供一个空的NodeSeq,前提是没有selectedUser,否则将为绑定结果.
This will give you an empty NodeSeq iff there is no selectedUser and the result of the bind otherwise.
def userdetail(html: NodeSeq):NodeSeq = {
selectedUser.is.toList.flatMap{user => bind("user", html,
"username" -> {user.username},
"firstname" -> {user.firstname},
"lastname" -> {user.lastname},
"lastloggedin" -> {user.lastloggedin})}
}
这篇关于Scala-提升-映射自定义盒装对象以进行绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!