Lift Ajax basic question. - Will really appreciate the help. Thank you.

67 views
Skip to first unread message

Global Values Inc

unread,
Feb 10, 2012, 4:42:47 PM2/10/12
to lif...@googlegroups.com

I have to admit I am not able to understand the way Lift handles Ajax. Please forgive me for my silliness regarding this. I have read several posts and read book on Lift Ajax. But I am still having a problem to understand clearly how it works. That is mainly because of the way I learnt to understand Ajax or what programming background I am from. Please forgive my lack of understanding and I request your help.

I think one of my problems might be all the Ajax articles or samples are also explaining how to do certain other things (for example UI with CSS transforms) when explaining how Ajax works in lift. Being a new comer (or due to some limitations due to my background) I am getting lost. To explain my confusion, I have made a specific question below. If I know that answer I think I can get much better understanding from the samples. Please see the jQuery example below.


$.get('login.php',formData,processData);


This is an example of typical Ajax call in jQuery (get function). This code passes three arguments to the get() function. The first—‘login.php’— is a string identifying where the data should be sent—in this case, a file on the server named login.php. The second argument is the query string containing the data that’s being sent to the server—the login information. Finally, process- Data refers to the callback function that will process the server’s response.

 

I want to understand the Lift side abstractions that map to the following

1.     get

2.     server side file or script that will handle this ajax call (login.php) here.

3.     formData – Arguments that go the server

4.     processData – The call back function.

If I know what Lift side features map to these that will provide me the clarity on Lift Ajax. Thank you very much.


-Anand

David Pollak

unread,
Feb 10, 2012, 5:04:36 PM2/10/12
to lif...@googlegroups.com
Lift does things differently because Lift intentionally abstracts the plumbing of an Ajax request... kind of like how Visual Basic abstracts button presses, etc.

First, you could write Lift code to handle the above request using RestHelper:

serve {
  case "login" :: Nil Get _ => JsonResponse(true)
}

That would return a "true" JSON value to any GET request to /login

But in Lift, setting up routes, etc. is both a lot of make-work and leads to insecurities because well-known URLs have to be heavily guarded against attacks where single-use URLs cannot be used except by the current session.  In order to make that abstraction, Lift associates a function on the server side with a GUID on the client side.  When the GUID is sent to the server, the function is evaluated and because Scala functions close over local scope, you get something that feels like a continuation.

So, the most simple example is:

SHtml.ajaxButton(<b>Click Me</b>, () => {println("Somebody clicked the button"); Alert("You clicked the button")})

That creates a button that, when clicked, will execute the function on the server side.  The function prints out a message and then returns a JsCmd that pops up an Alert() on the client.

There are many different controls you can create and things you can associate with functions on the server side.  But the key aspect is that you're not worrying about the plumbing... the creation of the HTTP request or the route, you're simply associating the client-side "thing" with the server-side code.

There are extra things that Lift does to insure optimal behavior for Lift-generated Ajax calls.  First, all Ajax calls are serialized. This means that you can't flood the browser with Ajax requests (this is particularly important on IE6 which only allows 2 connections to the server per client and if an Ajax call winds up forcing loading more content like an image from the server, having the connections flooded is a real problem.)  Lift puts up a little spiny thing during Lift-managed Ajax calls... automatically for you.  If a Lift Ajax request fails, there are back-offs and repeat counts so that if there's intermittent HTTP failures, your app need not worry about retry logic, etc.

I hope this helps to address your question.

Thanks,

David
 


-Anand

--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code



--
Visi.Pro, Cloud Computing for the Rest of Us http://visi.pro
Lift, the simply functional web framework http://liftweb.net


Global Values Inc

unread,
Feb 10, 2012, 5:47:34 PM2/10/12
to lif...@googlegroups.com

David,


Thank you for your quick response.


First I want to explain one of the foremost reasons why I am interested in Liftweb is its built-in security. I was entrusted with turning around the security holes of a 50 Million SaaS product developed in some legacy Microsoft web technologies and .net. I know how much of an effort it was in terms of turning around developers mentality and the training that has to take place. Even though we made our product secured we could not do things like having GUID based transaction between the client and server. I feel Liftweb’s genius-ness is the having security incorporated in its core and making it out of the box. This is my primary reason to start looking at Lift. Also the overall architecture is enterprise class. It is as elegant framework as Scala being a concise and productive programming language. Whenever I required any information pushed to browsers from server my development team will say all sorts of stories and I would give up the feature as it is not worth the effort. Here you have comet out of the box. So I very well understand the power and supremacy of Lift, I am very appreciative of this framework and thankful that such wonderful innovation is open source. Also the community is very passionate. You can sense that every day. You cannot build or inspire such a community without the architectural strength of the product.


Coming back to your answer, I have to look at SHtml object and its documentation to understand Ajax get and post signatures and start familiarizing with Ajax to start with and start benefitting the extra powers of Ajax. Can I assume http://scala-tools.org/mvnsites/liftweb-2.0/framework/scaladocs/net/liftweb/http/SHtml$object.html link to be good starting point to understand more about sHtml? Or if you have more resources on this, please point them to me.  Thank you again.


-Anand

Global Values Inc

unread,
Feb 10, 2012, 5:58:31 PM2/10/12
to lif...@googlegroups.com
David,

Actually I see the latest documentation here.


Thanks.

-Anand

Aditya Vishwakarma

unread,
Feb 11, 2012, 3:18:59 AM2/11/12
to lif...@googlegroups.com
Actually 2.4-M4 is a milestone release. use this one: http://scala-tools.org/mvnsites/liftweb-2.4/#net.liftweb.http.SHtml

And yes Lift is quite amazing. At our startup, we love lift.

Aditya Vishwakarma

Sofoklis Papasofokli

unread,
Feb 11, 2012, 8:14:36 AM2/11/12
to lif...@googlegroups.com
Anand,

I wrote a short blog with sample code based on jQuery

http://sofoklis.posterous.com/lift-ajax-very-basics

Maybe it helps.

Sofoklis
Sofoklis

Anand

unread,
Feb 11, 2012, 12:36:04 PM2/11/12
to lif...@googlegroups.com
Thank you. It is very helpful.

Best Regards,

Anand S. Anandan

Sent from my iPad

Anand

unread,
Feb 11, 2012, 12:36:28 PM2/11/12
to lif...@googlegroups.com
Thank you.


Best Regards,

Anand S. Anandan

Sent from my iPad
Reply all
Reply to author
Forward
0 new messages