Play With Liftweb’s AJAX Generators using Scala

AJAX is an approach for building dynamic web content on the client side . In Liftweb , we can implement this approach either by AJAX or by Comet . Comet is like AJAX , except in opposite direction .
AJAX model increases the richness of the User Experience for a single client at a time, Comet can do the same for multiple users.
First thing , I want to point out here is that AJAX generators take callback methods just like regular element generators. The only Difference is that when we call SHtml generator , it always returns SHtml.any . On the other way , when we call AJAX , it must returns a net.liftweb.http.js.JsCmd. The reason is that the return from the callback is itself a client-side callback that can be used to update the client content.

Let’s have a look at some AJAX generators :

1) ajaxButton : Renders a button that will submit an AJAX request to server .

def ajaxButton(text: String,func: () => net.liftweb.http.js.JsCmd,attrs: 
 net.liftweb.http.SHtml.ElemAttr*): scala.xml.Elem 

2) ajaxText : Renders an input text element that will send an AJAX request on blur.

def ajaxText(value: String,func: String => net.liftweb.http.js.JsCmd,attrs: 
 net.liftweb.http.SHtml.ElemAttr*): scala.xml.Elem 

This is very useful when you want to get text box value , once user hits enter key .

3)ajaxCall: Renders the JavaScript code that will submit an AJAX request but it will also send the value returned by the JsExp provided.

def ajaxCall(jsCalcValue: net.liftweb.http.js.JsExp,func: String => 
 net.liftweb.http.js.JsCmd): net.liftweb.http.GUIDJsExp 

In jsCalcValue , you can get only one html element value by id using JE.ValById(“test”) and pass this value to java script function .

4)ajaxInvoke: Similar to ajaxCall but there is no value to be computed and sent to the server

def ajaxInvoke(func: () => net.liftweb.http.js.JsCmd): net.liftweb.http.GUIDJsExp 

5)ajaxRadio: Renders a radio element then sends an AJAX call when the value changes

def ajaxRadio[T](opts: Seq[T],deflt: net.liftweb.common.Box[T],ajaxFunc: T => 
 net.liftweb.http.js.JsCmd,attrs: net.liftweb.http.SHtml.ElemAttr*): 

6)ajaxSelect: Renders a select element then sends an AJAX call when the value changes

def ajaxSelect(opts: Seq[(String, String)],deflt: net.liftweb.common.Box[String],func: String => 
 net.liftweb.http.js.JsCmd,attrs: net.liftweb.http.SHtml.ElemAttr*): scala.xml.Elem

7)ajaxCheckbox: Renders a checkbox element that when clicked will send an AJAX call

def ajaxCheckbox(value: Boolean,func: Boolean => net.liftweb.http.js.JsCmd,attrs: 
 net.liftweb.http.SHtml.ElemAttr*): scala.xml.Elem

8)ajaxForm: Wraps a NodeSeq that represents the form’s content and makes an AJAX call when the form is submitted.

def ajaxForm(body: scala.xml.NodeSeq): scala.xml.Elem

9)makeAJAXCall: Renders the JavaScript code that will submit an AJAX request

About Ayush Mishra

Ayush is the Sr. Software Consultant @ Knoldus Software LLP. In his 5 years of experience he has become developer with proven experience in architecting and developing web applications. Ayush has a Masters in Computer Application from U.P. Technical University, Ayush is a strong-willed and self-motivated professional who takes deep care in adhering to quality norms within projects. He is capable of managing challenging projects with remarkable deadline sensitivity without compromising code quality. .
This entry was posted in Java, LiftWeb, Scala, Web. Bookmark the permalink.

One Response to Play With Liftweb’s AJAX Generators using Scala

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s