views:

112

answers:

2

I have a search box that sends an ajax request to a snippet. When the snipped function gets called I would like the search query to get added to the URL via a anchor hash (ala: http://localhost/search#param) so I can recreate the search request if the user copies the URL and comes back later. Obviously the other side to this is pulling the anchor field in the snippet so the search can be recreated as necessary. I am a bit lost as to how to do this..not even sure where a good starting point is. Any tips or hints on how to do this in Lift would be very welcome.

A: 
LiftRules.satelessRewrite.append{
case RewriteRequest(ParsePath(List("path","to", myParam), _, _, _(, _, _) => RewriteResponse("path" :: "to" :: Nil, Map("myParam" -> myParam))
}

Note that I copied this, and did not paste. so you probably should look in the book if it doesn't work.

Jim Barrows
+2  A: 

I just got this working.. here's my template:

<lift:surround with="default" at="content">
    <lift:searchSnippet.search>
        <s:query/>
        <s:submit/>
    </lift:searchSnippet.search>
</lift:surround>

And my snippet:

class SearchSnippet extends Logger {

  val ns = "s"

  def search(in: NodeSeq) : NodeSeq = {
    // save the url of the original page request
    val url = S.uriAndQueryString.openOr("")

    var query = ""
    def submit : JsCmd = {
      JsRaw("window.location.href = '" + url + "#" + query + "'")
    }
    SHtml.ajaxForm({
      bind(ns, in,
        "query" -> SHtml.text(query, { q =>
          query = q
        }),
        "submit" -> (SHtml.submit("Search", () => {}) ++ SHtml.hidden(submit _)))
    })
  }
}

The trick is to save the url when the snippet originally renders so that you don't get the ajax_request/... uri from which the ajax form submits.

Collin
Doesn't recreate / execute requests for URLs which already contain a # value.
Allyn
Anchor tags do not get submitted to the server by the browser. You , though, can grab this anchor tag using Javascript (location.hash.substring(1)) and then making a body#onLoad call to grab the results for the query.
Collin