Kevin Mansel
Kevin Mansel

Reputation: 2371

Scala - Lift - map a custom boxed object for bind?

Ok, so i have a custom object that is a 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})}
}

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 :

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.

Thanks!

Upvotes: 1

Views: 365

Answers (1)

Kim Stebel
Kim Stebel

Reputation: 42037

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

Upvotes: 2

Related Questions