Something you might find useful: easy lazy loading of page sections

9 views
Skip to first unread message

harryh

unread,
Aug 26, 2009, 5:44:50 PM8/26/09
to Lift
Let's say you have a bit of a page like so:

<lift:SomeSnippet.section>
<foo:name/>
<lift:SomeSnippet.section>

Now, let's assume that computing whatever is bound to <foo:name/>
takes a long time (maybe it takes a network call, or a long database
query, or whatever). You don't want to hold up your entire page view
on that so.....

<lift:Util.lazyLoad>
<lift:SomeSnippet.section>
<foo:name/>
<lift:SomeSnippet.section>
</lift:Util.lazyLoad>

Then in your Util snippet:

def lazyLoad(xhtml: NodeSeq) = {
val id = "lazy"+System.currentTimeMillis()
val (name, exp) = ajaxInvoke(() => { SetHtml(id, xhtml) })
<div id={id}>
<img src="/img/ajax_spinner.gif" height="32" width="32" alt="wait"/
>
{Script(OnLoad(exp.cmd))}
</div>
}

Feel free to critique if there is a better way of doing this. It's
working pretty good for me so far though.

-harryh

David Pollak

unread,
Aug 27, 2009, 12:59:57 AM8/27/09
to lif...@googlegroups.com
This is nifty... I think it's worthy of inclusion in Lift.  Thanks for the suggestion!
--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp

Naftoli Gugenheim

unread,
Aug 27, 2009, 1:29:53 AM8/27/09
to lif...@googlegroups.com
Neat!
Maybe a better name would be asyncLoad?

-------------------------------------

Charles F. Munat

unread,
Aug 27, 2009, 5:54:53 PM8/27/09
to lif...@googlegroups.com
Nice!

Derek Chen-Becker

unread,
Aug 31, 2009, 12:24:27 PM8/31/09
to lif...@googlegroups.com
Seems like this would be a pretty useful function to have expose as a snippet tag attribute (like how we have eager_eval). Maybe an "async_load" attr and a corresponding LiftRule NodeSeq var that controls what gets displayed while waiting?

Derek

David Pollak

unread,
Aug 31, 2009, 12:47:06 PM8/31/09
to lif...@googlegroups.com
On Mon, Aug 31, 2009 at 9:24 AM, Derek Chen-Becker <dchen...@gmail.com> wrote:
Seems like this would be a pretty useful function to have expose as a snippet tag attribute (like how we have eager_eval). Maybe an "async_load" attr and a corresponding LiftRule NodeSeq var that controls what gets displayed while waiting?

Good idea... added this to the ticket: http://github.com/dpp/liftweb/issues#issue/56
 
Reply all
Reply to author
Forward
0 new messages