Hi, thanks for the suggestions I like that match  {} code.. will try
that..

But what you suggest but the post_id is similar to what I'm doing
now.. since this id is a long it will be copied by value
and I can pass it to multiple requests using a closure...

I'll try to clarify a bit :

A have  a ViewClient snippet and an AddInvoice snippet

In the ViewClient I add the action AddInvoice  which must be bound to
a client..

Now passing the client to the AddInvoice form is ok .. I can send the
whole client object to the form snippet using a RequestVar..
which I find kinda neet not having to go trough thoses ids... (maybe
i'm wrong thinking that)

But the trick is now that i'm in the AddInvoice snipped and that I
have my RequestVar...

I would like to form a closure on it.. an keep it for the post request
so that it gets to ProcesssEntry and I do just :

inInvoice.client (client)
inInvoice.save

As so no ids are involved...

I would need to make a new RequestVar for the post submit with a copy
of the input RequestVar I got in the AddInvoice from the ViewClient
snippet

But a SessionVar is out it's way overkill and a statefull snippet....
might be an idea ... would the RequestVar persist in the statefull
snippet?

Also I'm having trouble understanding how that scoping is done for the
RequestVar if anyone could shed some light on it... like why a closure
won't "ref" it... (I'm new to scala)

Thanks

hexa


On Mar 9, 6:21 am, Lukasz Kuczera <kuk...@gmail.com> wrote:
> 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