2016-01-24 86 views
2

我試圖在我的應用程序中建立一些相當複雜的麪包屑功能,其中多個頁面可能鏈接到一個詳細信息頁面,但我只想顯示詳細信息頁面的麪包屑,如果通過特定路線到達頁面。如何檢查URL是否與Lift中現有的Loc匹配?

在我的用例中,用戶會轉到搜索頁面並鍵入搜索字符串。表單在當前頁面上使用「get」方法爲用戶呈現包含一些項目的搜索結果。用戶選擇一個項目以深入其詳細信息頁面。在詳細信息頁面上,我檢查S.referer,發現它是一個字符串:http://localhost:8080/myapp/search?q=Query+Data+Here

有沒有什麼辦法可以採取Search頁面Loc並測試上面的String URL是否與它匹配?現在我正在執行此檢查,只需在引用字符串上運行一個包含並根據結果執行行爲。

這是我目前的執行:

/** 
* List of valid pages to use for generating the page breadcrumbs. 
*/ 
private def validParentLocations = Seq("Search", "Browse") 

/** 
* If the referer to this page is one of the valid parent locations, 
* then find the a tag with the "prev" id and route it to the referer. 
* 
* If the referer to this page is not in the list or empty, do not 
* display the breadcrumb component. 
*/ 
def breadcrumb = { 
    S.referer match { 
    case Full(reference) => 
     validParentLocations.find(s => reference.contains(s"myapp/${s.toLowerCase}")).map(parent => 
     "#prev *" #> parent & 
     "#prev [href]" #> reference 
    ).getOrElse(ClearNodes) 
    case _ => ClearNodes 
    } 
} 

正如你所看到的,我希望能更換validParentLocations是祿的,而不是如果我修改頁面的定義在Boot這可能會破壞脆弱的弦。有沒有辦法基本上說myPageLoc.checkIfUrlMatches(string: String): Boolean或我失蹤的匹配模式?有沒有更優雅的方式來使用Lift中的現有功能來完成此操作?

回答

0

經過一段時間的忙碌之後,我發現了一種方法,通過使用共享LocGroup名稱將Loc註冊爲Detail頁面的有效引用者。現在,我可以抓取頁面的所有有效推介鏈接,並調用他們的默認href函數來測試它們是否匹配 - 仍然覺得可能有更好的方法...任何與我的網站匹配的推薦鏈接都可以通過。

下面的代碼:

Boot.scala:

<...> 
Menu.i("Search")/"myApp"/"search" >> LocGroup("main", Detail.referralKey), 
Menu.i("Browse")/"myApp"/"browse" >> LocGroup("main", Detail.referralKey), 
Detail.getMenu, 
<...> 

Detail.scala:

<...> 
def referralKey = "detail-page-parent" 

/** 
* Sequence of valid Locs to use for generating the page breadcrumbs. 
*/ 
private def validParentLocations = LiftRules.siteMap.map(site => site.locForGroup(locGroupNameForBreadcrumbParents)) openOr Seq() 

/** 
* If the referer to this page is one of the valid parent locations, 
* then find the a tag with the "prev" id and route it to the referer. 
* 
* If the referer to this page is not in the list or empty, do not 
* display the breadcrumb component. 
*/ 
def breadcrumb = { 
    S.referer match { 
    case Full(reference) => 
     validParentLocations.find(loc => reference.contains(loc.calcDefaultHref)).map(parent => 
     "#prev *" #> parent.name & 
     "#prev [href]" #> reference 
    ).getOrElse(ClearNodes) 
    case _ => ClearNodes 
    } 
} 
<...> 
相關問題