Search code examples
bindinglift

binding Scala to HTML in Lift


I've started playing with Lift. I want to create a basic application which takes input from user via input field, performs a db search on that input, and takes the results of that db search and creates a List of objects which then it binds to some html code.

I need help only with the binding part, but some background info first.

I have a List[Node]:
case class Node(aname:String, adata:String) {..}

I have this html to bind to:

<div class="lift:helloWorld.displayNodes">
    <div>
            <div><li><B>Node:</B></li></div>
                <div>Name: <node:name></node:name></div>
                <div>Data: <node:data></node:data></div>
                <BR>
        </div>
</div>

The problem is that this code calls my method displayNodes from the snippet and passes itself in. That way I can bind to it because I have the html code. So I bind like this:

//this method is called from the index.html and 
def displayNodes(html : NodeSeq) : NodeSeq = {
    nodeList.flatMap {node => Helpers.bind("node", html, "name"->node.name, "data"->node.data) }
}

Now the Question:
How do I bind without specifying in the html which method I want the html code to call. I mean I want to remove this line: <div class="lift:helloWorld.displayNodes"> because I don't want to have it called until the list is being populated. So basically instead of the html "calling" the scala code I want the scala code to call the html and bind to it, but only after it has the data. How can this be done?

PS - I looked at cookbook.liftweb.net, but they don't have an example of what I need.


Solution

  • I'd suggest to use SHtml.idMemoize http://lift.la/blog/shtmlidmemoize-simple-ajax-updating This way you can initially have a page with an input and no results. After the user inputs his data and the server get's the DB data, you update the results by quering outer.setHtml

    It's cheaper and easier than to set up a CometActor, but you'll get the results only when the user asks something. So, this way server won't be able to push some updates to the browser by itself.