First bit of Lift 3.0 February 12, 2013
Lift is growing
Lift is growing and evolving.
I've just started the Lift 3.0 code branch. Lift 3.0 will be based on Scala 2.10+ and will use features exclusive to 2.10 including macros. Lift 3.0 will also cut away at a lot of cruft that's grown onto Lift over the years, so 3.0 will have a bunch of breaking changes.
The Future is Futures
Lift 3.0 will support Futures (specifically LAFutures which are Lift's time-tested, solid Futures) such that you can do stuff like this in a REST call:
object DelayedRest extends RestHelper {
serve {
case "delay" :: Nil Get _ =>
LAFuture(() => {
Thread.sleep(2000)
<b>Hello</b>})
}
}
When Lift detects that an LAFuture
is servicing being returned from the REST
call, Lift will automatically use the Container's asynchronous support, suspect the
request thread, and only restart the request thread when the answer is available.
There will be other places in the code where Lift automatically works with Futures.
Any data-
attribute can be processed in Lift
Lift's Designer Friendly Templates have been totally awesome. And with 3.0, the awesomeness is extended.
Lift's Data Attribute Processing allows you to hook into Lift's templating and process any data attribute, like so:
LiftRules.dataAttributeProcessor.append {
case ("wombat", str, nodes) => ("div *+" #> str).apply(nodes)
}
Which will transform:
<div data-wombat="David">My wombat is named: </div>
Into:
<div>My wombat is named: David</div>
Data Attribute Processors also deal well with Futures, so:
LiftRules.dataAttributeProcessor.append {
case ("ad-network", service, _) => LAFuture(() => getAdsFor(service))
}
Will not block the main rendering thread, but will be computed off-thread and the result will be integrated onto the page when the result becomes available.
There are a ton of uses for Data Attribute Processing for creating project specific templating for HTML folks to do lots of stuff server-side.
Acting across Address Spaces
Actors are cool. They are a nice asynchronous mechanism for processing data.
Wouldn't it be nice if you had client-side actors (a JavaScript functionâ„—
that when called would serialize the object as JSON and send it to an Actor on
the server)?
Wouldn't it be cool if there was a server-side proxy of a function on the client such that when you send JSON-serializable data to the server-object, it gets JSON serialized and sent to the client?
Well, Lift 3.0 bridges address spaces for object between the browser and the server. Yep, that's right, Lift 3.0 is the first web framework to support distributed objects across browser and server.
Here's how it's done:
// get a server-side actor that when we send
// a JSON serializable object, it will send it to the client
// and call the named function with the parameter
val clientProxy = sess.serverActorForClient("changeNode")
// Create a server-side Actor that will receive messaes when
// a function on the client is called
val serverActor = new ScopedLiftActor {
override def lowPriority = {
case JString(str) => clientProxy ! ("You said: "+str)
}
}
Script(JsRaw("var sendToServer = "+sess.clientActorFor(serverActor).toJsCmd).cmd &
JsRaw("function changeNode(str) {document.getElementById(\"foo\").innerHTML = str;}").cmd)
So, yes, it's that simple. clientProxy ! "Hi"
will serialize the String and invoke the changeNode
function on the client with the string.
Call sendToServer
from JavaScript on the client and voila, the Actor on the server-side
gets a message.
So, on the client, we say:
<button onclick="sendToServer(document.getElementById('in').value)">Click me</button>
And we get a server-round-trip.
The code
You can see sample code on GitHub.
Thanks!