TabVar anyone? (like SessionVar, but per browser tab)

206 views
Skip to first unread message

Diego Medina

unread,
May 17, 2011, 1:55:54 PM5/17/11
to Lift
Hi,

Maybe this is already in place and I just missed it.
I have a page with a form, this page has a named Comet Actor, the name
is saved on a SessionVar, so that the snippet that does the render of
the form can access the name of the comet actor. Once you submit the
form, the comet actor name is pass to a LiftActor as part of the
message to process. Once the LiftActor finishes his work, it goes and
tells the named actor to display a message on the browser.

Using this technique, I can have my page loaded on Firefox and another
instance of this page on Safari, and they do not step on each other,
the comet actor will only update the browser that submitted the form.
Well, unless you have two tabs open on FF, in which case both tabs get
the message.

So I thought that if I had a TabVar, I could store the comet name
there, and each tab on FF would have its own instance. Or how can I
achieve this?

At first I used a RequestVar, but on page reload (form submitted), I
would loose the value.

I set the value of the SessionVar if it is empty, if present, I use
the name stored there.

Thanks

Diego

P.S. sample app is here:

https://github.com/fmpwizard/lift-conditional-drop-down-menus

it is the LiftActor link on the left menu.


--
Diego Medina
Web Developer
http://www.fmpwizard.com

Antonio Salazar Cardozo

unread,
May 17, 2011, 2:36:44 PM5/17/11
to lif...@googlegroups.com
I don't think there's a good way of distinguishing between different tabs in browsers, since all requests come loaded with the same cookies and such even if they come from different tabs. The only way to distinguish is, I believe, by distinguishing between different pages, e.g. by having a different comet actor on every render (using a different name). I know I've seen that approach recommended (basically appending a unique string to the end of the actor name to ensure that it is unique per-render).

In those cases, you would also set a lifeSpan on the comet so that it would go away after a certain amount of time of not being on-page.

But, maybe there's a cool tricksy way to do it I don't know about!
Thanks,
Antonio

Diego Medina

unread,
May 17, 2011, 2:42:24 PM5/17/11
to lif...@googlegroups.com
Hi Antonio,

That was the way I first tried, but my problem was that after I
submitted the form, I would get a new comet actor, and my liftactor
had no idea of this new name.

(btw, I removed a dependency to reactive web on my sample app)

Thanks

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

David Pollak

unread,
May 17, 2011, 2:46:22 PM5/17/11
to lif...@googlegroups.com
On Tue, May 17, 2011 at 11:42 AM, Diego Medina <di...@fmpwizard.com> wrote:
Hi Antonio,

That was the way I first tried, but my problem was that after I
submitted the form, I would get a new comet actor, and my liftactor
had no idea of this new name.

I'd use a RequestVar and put a hidden field in the form to snapshot/restore the RequestVar on the form submission.


 



--
Lift, the simply functional web framework http://liftweb.net

Diego Medina

unread,
May 17, 2011, 2:53:10 PM5/17/11
to lif...@googlegroups.com

Ah, I now remember you posted something like that before, and note I get to use it :)

Thanks!

Sent from my cell
Diego Medina

Naftoli Gugenheim

unread,
May 17, 2011, 3:41:12 PM5/17/11
to lif...@googlegroups.com
Part of the idea of reactive-web is so that you shouldn't have to do this manually.


Diego Medina

unread,
May 17, 2011, 3:47:00 PM5/17/11
to lif...@googlegroups.com
On Tue, May 17, 2011 at 3:41 PM, Naftoli Gugenheim <nafto...@gmail.com> wrote:
> Part of the idea of reactive-web is so that you shouldn't have to do this
> manually.

Once I publish a post about using just Lift, I'll work on another
sample to see how reactive would do the same. Then people will have
more options and can choose the way they feel fit their needs.

Regards,

Diego

Diego Medina

unread,
May 18, 2011, 12:19:25 AM5/18/11
to lif...@googlegroups.com
Just for completeness, this is the code I ended up using to store the
name of the comet actor on a RequestVar and use the snapshot
technique:

object cometName extends RequestVar[Box[String]](Empty)


def render ={
...
def snapshot(): () => Unit = {
val cn = cometName.get
() => {cometName.set(cn)}
}

...
"type=hidden" #> SHtml.hidden(snapshot())

}

And it works beautifully!

Now to write a longer post explaining this and my initial work with LiftActors

Thanks

Diego

David Pollak

unread,
May 18, 2011, 8:32:14 AM5/18/11
to lif...@googlegroups.com
On Tue, May 17, 2011 at 9:19 PM, Diego Medina <di...@fmpwizard.com> wrote:
Just for completeness, this is the code I ended up using to store the
name of the comet actor on a RequestVar and use the snapshot
technique:

object cometName extends RequestVar[Box[String]](Empty)


def render ={
...
 def snapshot(): () => Unit = {
     val cn = cometName.get
     () => {cometName.set(cn)}
 }


Cool.  Note that 2.4 has RequestVarSnapshotGroup:

case object MyTabGroup

object MyVar1 extends SnapshotRequestVar(MyTabGroup, "foo")
object MyVar2 extends SnapshotRequestVar(MyTabGroup, 42)
 
def myMethod = {

  val snapshot: () => Unit = RequestVar.snapshot(MyTabGroup) // when this function is applied, it will restore the values of all the members of the MyTabGroup
}








--

Diego Medina

unread,
May 18, 2011, 11:09:40 AM5/18/11
to lif...@googlegroups.com
On Wed, May 18, 2011 at 8:32 AM, David Pollak
<feeder.of...@gmail.com> wrote:
>
>
> On Tue, May 17, 2011 at 9:19 PM, Diego Medina <di...@fmpwizard.com> wrote:
>>
>> Just for completeness, this is the code I ended up using to store the
>> name of the comet actor on a RequestVar and use the snapshot
>> technique:
>>
>> object cometName extends RequestVar[Box[String]](Empty)
>>
>>
>> def render ={
>> ...
>>  def snapshot(): () => Unit = {
>>      val cn = cometName.get
>>      () => {cometName.set(cn)}
>>  }
>>
>
> Cool.  Note that 2.4 has RequestVarSnapshotGroup:
>
> case object MyTabGroup
>
> object MyVar1 extends SnapshotRequestVar(MyTabGroup, "foo")
> object MyVar2 extends SnapshotRequestVar(MyTabGroup, 42)
>
> def myMethod = {
>
>   val snapshot: () => Unit = RequestVar.snapshot(MyTabGroup) // when this
> function is applied, it will restore the values of all the members of the
> MyTabGroup
> }
>

Thanks for adding this

Reply all
Reply to author
Forward
0 new messages