Add lazy loading 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.
<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.
Leave a comment
on 2010-02-06 11:48 *
By github.importer
Imported from GitHub: http://github.com/dpp/liftweb/issues/56/find