2012-10-01 26 views
0

我試圖做/作者的簡單情況/和獲得提升根據傳入的ID,以建立一個Person對象問題電梯框架 - 與傳遞的URL參數去摘錄類

目前,我有一個作者片段

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

什麼,我試圖做的就是讓代碼來構建一個盒裝的人對象,並把它傳遞到Author類的構造函數。在渲染方法中,我想確定該框是否已滿並根據需要進行。

如果我改變

class Author(item: Person) { 

class Author(item: Box[Person]) { 

它不再起作用,但如果我離開它,因爲它是不再有效,完整(項目)不正確。如果我刪除了val s行,它會起作用(並用item.name替換s)。那麼我該如何做到這一點。謝謝

回答

0

從findPersonById(id:String)返回的Box:Box [Person]被評估,如果Box是Full,則取消裝箱值被傳遞到你的函數中。如果該框爲空或失敗,則應用程序將顯示一個404或適當的錯誤頁面。

如果你想處理這個錯誤檢查你自己(所以這個方法的結果總是一個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) 
    } 
} 

,然後這應該工作:

class Author(item: Box[Person])