2016-01-24 63 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 
    } 
} 
<...> 
相关问题