Christian Oestreich

   two guys in design - software.development.professional

Lift Me Up

| Comments

I have been working on Scala and Lift for a few weeks now and wanted to post some of my code samples here and hopefully they are useful to someone else in the future.

I would like to first say thank you to all those involved with developing and creating the liftweb demos.  I know doing that can be a huge PITA, so Thanks!  I would simply like to ask that future versions of demo code include the html file code that produces the UI output.  Having only the backend code causes a bit of stumbling.

The first examle I worked on was based on the Ajax (http://demo.liftweb.net/ajax) sample from liftweb.  Here is the code below.

File: ajax.html

1
2
3
4
5
6
7
8
9
<lift:surround with="default" at="content">
 <div>
 <lift:AjaxSample.sample>
 <ajax:clicker>Click here to update count <span id="cnt_id">0</span></ajax:clicker>
 <br />
 <ajax:auto limit="20" />
 </lift:AjaxSample.sample>
 </div>
</lift:surround>

File: AjaxSample.scala

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import net.liftweb.http._
import js.jquery.JqJsCmds.DisplayMessage
import js.JsCmds.SetHtml
import S._
import SHtml._
import net.liftweb.util._
import Helpers._
import scala.xml._
import net.liftweb.http.js._
import net.liftweb.common.{Empty, Logger, Full}
import java.lang.String
import net.liftweb.widgets.autocomplete.AutoComplete

class AjaxSample {
 def sample(xhtml: NodeSeq): NodeSeq = {
 // local state for the counter
 var cnt = 0
 // get the id of some elements to update
 val spanName: String = S.attr("id_name") openOr "cnt_id"
 val msgName: String = S.attr("id_msgs") openOr "messages"
 // build up an ajax <a> tag to increment the counter
 def doClicker(text: NodeSeq) =
 a(() => {cnt = cnt + 1; SetHtml(spanName, Text( cnt.toString))}, text)
 // bind the view to the functionality
 bind("ajax", xhtml,
 "clicker" -> doClicker _,
 "auto" -> AutoComplete("", buildQuery _, _ => ()))
 }

 private def buildQuery(current: String, limit: Int): Seq[String] = {
 Log.info("Checking on server side with "+current+" limit "+limit)
 (1 to limit).map(n => current+""+n)
 }
}

All that this example does is simply do the a href tag ajax click to increment the number and builds a autocomplete box that will simply show 10 of whatever text you are typing retrieved via ajax of course.

I will keep putting my code samples up here as I create them.

Cheers

Comments