IMHO you have three options:
1. Use statefull snippet
2. Use SessionVar.
3. User RequestVar.

I went for third option. Because I don't need to retain this value
between multiple requests.

In my code i use something like:
object Index {
        object postidVar extends RequestVar(S.param("postid").map(_.toLong)
openOr 0L)
        def postid: Long = postidVar.is
}

This gives me global access to RequestVar. Then i use it with
something like:
        /**
         * Renders post in details.
         * @param in
         * @return
         */
        def show(in: NodeSeq): NodeSeq = {
                Post.find(Index.postid) match {
                        case Full(post) => bind("post",in,
                                "title"->post.title,
                                "text" -> post.text,
                                "date" -> (new SimpleDateFormat(Const.format) 
format
post.date.get))

                        case Empty => Text("No such post")
                        case Failure(_,_,_) => S.redirectTo("/failure.html")
                }
        }

I set it with:

if(User.loggedIn_?) SHtml.link("/edit", ()=> Index.postidVar(post.id),
Text("Edit"))  else Text("")

Or with:

SHtml.link("/details.html",()=>Index.postidVar(post.id),Text("Read
more"))

And in another snipped I use it like:

        def edit(in: NodeSeq): NodeSeq = {
                var title = ""
                var text = ""
                var tags = ""
                var post = Post.find(Index.postid)

                def submit() = {
                        if(title=="") S.error("Title musn't be empty")
                        else {
                                post.open_!.title(title).text(text).save
                                S.redirectTo("/index")
                        }
                }
                post match {
                        case Full(p) => bind("post",in,
                                        "title" -> SHtml.text(p.title, parm => 
title=parm,
("size","55")),
                                        "tags" -> SHtml.text("", parm => 
tags=parm),
                                        "text" -> SHtml.textarea(p.text, parm 
=> text=parm),
                                        "submit" -> SHtml.submit("Save", submit)
                                        )
                        case Empty => S.error("Post to edit not found"); 
S.redirectTo("/
index")
                }

You can check the code here:
http://github.com/kukems/lift-blog


What i would suggest to change clientBox definition from
object currentClient extends RequestVar [Box [Client]] (Empty)
to:
object currentClient extends
RequestVar(S.param("client_id").map(_.toLong) openOr -1L)

I assume that you don't have anything in database that match Client.id
== -1

Then you can use currentClient as:
Client findByKey currentClient.is map inInvoice.client (_)

On Mar 9, 7:13 am, hexa <hex...@gmail.com> wrote:
> Hi,
>   I have a RequestVar that I send to a snippet which will then do a
> post...
>
> But I would like the RequestVar to persist between the moment it it
> received in the post snippet and the post itself...
>
> The only way I found of doing it right now is like :
>
> Source Snippet :
>
> object ViewClient extends ViewClient
>
> class ViewClient
> {
>   object currentClient extends RequestVar [Box [Client]] (Empty)
>
> bind  (...
>   "addInvoice" -> SHtml.link ("/invoice/create", () => currentClient
> (Full (c)), Text ("Ajouter Facture")))
>
> Destination Post Snippet :
>
> def add (inhtml: NodeSeq) : NodeSeq = {
>
>     val inInvoice = Invoice.create
>     val clientBox = ViewClient.currentClient
>
>     val client_id = clientBox map (_.id.toLong)
>
>     def processEntry () {
>       Client.findByKey (client_id openOr 0) map (inInvoice.client (_))
>       inInvoice.save
>       S.notice ("Entre : Description " + inInvoice.description + "
> Montant : " + inInvoice.amount)
>     }
>
> bind ("e", inhtml,
>           "description" -> inInvoice.description.toForm,
>           "amount" -> inInvoice.amount.toForm,
>           "submit" -> SHtml.submit ("Ajouter Facture",  processEntry))
>
> If I try to access the clientBox in processEntry, even with the
> closure it should generate.. I get an empty box...
>
> Is there any way to copy / ref or anything or make a new RequestVar
> with a copy of the preceding one ?
>
> Would have been nice not to be obligated to do the client_id toLong
> code...
> And juste do inInvoice.client (client)  , inInvoice.save

You have to check it because it might be null/empty. Lift can't figure
out what to do when there is no value inside the Box. In Java typical
idiom is to return null which impose getting NullPointerException at
some point OR checking explicitly for null value which you do exactly
with openOr method.

> Thanks a lot
>
> hexa

-- 
You received this message because you are subscribed to the Google Groups 
"Lift" group.
To post to this group, send email to lift...@googlegroups.com.
To unsubscribe from this group, send email to 
liftweb+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en.

Reply via email to