A RequestVar is a container that transcends Scala's scope rules: multiple 
instances of the class holding the RequestVar will see the same value in their 
respective RequestVars.
On the other hand its value is per request. That has nothing to do with; two 
simultaneous requests can set and get two distinct values without conflict.
Contrast all this with regular scala variables. If a class defines a field, and 
spawns a thread that reads it later, clearly it is accessing the same field, 
even in Java. Somewhat similarly in Scala a closure (function) *captures* its 
references, possibly promoting them from stack to heap. So if a method a 
defines a val b and passes a function that references b to some API, b will 
outlive a's execution.
So Scala variables are limited by lexical scope but not by request, while 
RequestVars are limited by request but not by scope.
Thus, if a function will be executed in the next request, to bridge requests 
you have to store the *value* of the RequestVar outside of it (requestVar.is), 
then at the beginning of the next request set the RequestVar from that val.
This is a common pattern in Lift: one request specifies a function to be 
executed in the beginning of the next request, whether it is triggered by a 
link, a form submit, or redirect. This function can bridge the two requests 
because it's in the lexical scope of the first request, so to speak, and can 
capture its state with the basic rules of scala; but it since it actually 
executes in the next request, it can set RequestVars etc. that the next request 
will see.


-------------------------------------
hexa<hex...@gmail.com> wrote:

It is not set in the same request as it's being read exactly...

But how is that doable with RequestVars then ?



On Mar 9, 11:55 am, Naftoli Gugenheim <naftoli...@gmail.com> wrote:
> You can use StatefulSnippets if you like, but what you want to do is 
> perfectly doable with RequestVars.
> Clearly it's not being set in the same request as it's being read.
>
> -------------------------------------
>
> hexa<hex...@gmail.com> wrote:
>
> I think we still have a miss understanding
>
> The client_id code  clientBox map (_.id.toLong) work ok .. no problems
> with that
>
> What doesnt work would be doing something like :
>
>  val clientBox = ViewClient.currentClient
>
>   def processEntry () {
>       inInvoice.client (clientBox.open_!)
>       inInvoice.save
>       S.notice ("Entre : Description " + inInvoice.description + "
> Montant : " + inInvoice.amount)
>     }
>
> Since in this case inInvoice.client (clientBox.open_!) return an empty
> box since the RequestVar has been destroyed ...
>
> So I was trying to do this but I need to copy or ref the RequestVar so
> that I use it in the next request...
>
> At first I tought because of the closure it would ref it... but it
> seems not..
>
> It's looking more and more like a statefull snippet is the way to
> go...
> On Mar 9, 11:10 am, Lukasz Kuczera <kuk...@gmail.com> wrote:
>
>
>
> > Ok i think I see the "bug". It is the magic which i don't understand
> > yet that if you change your client_id from val to var it will be auto
> > promoted into Heap and live out Snippet. Try this out.
>
> > Change:
> > val client_id = clientBox map (_.id.toLong)
> > To:
> > var client_id = clientBox map (_.id.toLong)
>
> > On Mar 9, 3:34 pm, hexa <hex...@gmail.com> wrote:
>
> > > 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
>
> --
> 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 
> athttp://groups.google.com/group/liftweb?hl=en.

-- 
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.

-- 
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