How does T => JsCmd send data to Lift?
I remember my first few interactions with Lift’s ajax support, which were also my first few interactions with Scala. It was pretty hard to get my head around some of the method signatures I would find all across Lift’s source code.
The one that took me a while to understand was
I knew it worked because I would copy/paste code samples taken from the mailing list and they worked just as advertised.
Scala bits to clear your mind.
If you are new to Scala, the T there represents any type, it could be an
(String, String), etc. Let’s take, as an example, the type signature of
def ajaxRadio[T](opts: Seq[T], deflt: Box[T], ajaxFunc: T => JsCmd, attrs: ElemAttr*): ChoiceHolder[T] = ...
Those are a lot of
T types, just for now, let’s assume you are working with Strings, you could rewrite it in your head as:
def ajaxRadio(opts: Seq[String], deflt: Box[String], ajaxFunc: String => JsCmd, attrs: ElemAttr*): ChoiceHolder[String] = ...
The moment of the realization.
When the time comes to use ajaxRadio, you could define
and then you would write:
ajaxRadio(Seq("one", "dos", "tres"), Full("dos"), itemSelected _ )
so, once you select an item from the radio options, the function
itemSelected is called, and you get the selected value as a parameter to
itemSelected. Because this is an ajax* function, there is no page refresh going on.
Here, I am simply returning a
JsCmd.Noop, which does nothing on the UI, but you could return any other
JsCmd in there.
I hope this was helpful, and I’m planning on writing a follow up post with a full example on how to use ajaxRadio, as well as radioElem.
Thank you for reading and don’t hesitate to leave a comment/question.