0

I am trying to do a simple case of /author/ and get the Lift to build a Person object based on the id passed in.

Currently i have an Author snippet

    class Author(item: Person) {

       def render = {
       val s = item match { case Full(item) => "Name"; case _ => "not found" }

       " *" #> s;
       }
   }

object Author{

val menu = Menu.param[Person]("Author", "Author", authorId => findPersonById(authorId),  person => getIdForPerson(person)) / "author"

 def findPersonById(id:String) : Box[Person] = {

  //if(id == "bob"){
      val p = new Person()
      p.name="Bobby"
      p.age = 32
      println("findPersonById() id = " +id)
      Full(p)

  //}else{
     //return Empty
  //}


}

def getIdForPerson(person:Person) : String = {

  return "1234"
}
}

What i am attempting to do is get the code to build a boxed person object and pass it in to the Author class's constructor. In the render method i want determine if the box is full or not and proceed as appropriate.

If i change

class Author(item: Person) {

to

class Author(item: Box[Person]) {

It no longer works but if i leave it as is it is no longer valid as Full(item) is incorrect. If i remove the val s line it works (and replace the s with item.name). So how do i do this. Thanks

AdiFatLady
  • 368
  • 1
  • 6
  • 18

1 Answers1

0

The Box returned from findPersonById(id:String) : Box[Person] is evaluated and if the Box is Full, the unboxed value is passed into your function. If the Box is Empty or Failure the application will present a 404 or appropriate error page instead.

You can try double boxing your return if you want to handle this error checking yourself (so that the result of this method is always a Full Box).

def findPersonById(id:String) : Box[Box[Person]] = {
  if(id == "bob"){
      val p = new Person()
      p.name="Bobby"
      p.age = 32
      println("findPersonById() id = " +id)
      Full(Full(p))
  }else{
     return Full(Empty)
  }
}

and then this should work:

class Author(item: Box[Person]) 
jcern
  • 7,798
  • 4
  • 39
  • 47