RPC from Javascript/JQuery?

174 views
Skip to first unread message

Rick R

unread,
Feb 24, 2010, 11:23:57 AM2/24/10
to lif...@googlegroups.com
I have a textarea in which I process onKeyUp and onKeyDown commands.
The handlers for such things are custom javascript.
I would like to invoke functions in a Comet LiftActor /
ListenerManager via these custom javascript functions. Is there
documentation on the recommended way to do so?

It looks like I will have to define the functions within a render call
and use the SHtml.ajaxCall function, since the destination url is
randomized. I am just wondering what would be the idiomatic way to do
this.

This is for a chat style app which processes/distributes data by the
keystroke rather than by a line/post command.


I'm open to any ideas / alternate suggestions.

Thanks,
Rick

David Pollak

unread,
Feb 24, 2010, 2:57:43 PM2/24/10
to lif...@googlegroups.com
Rick,

Here's a simple example:

import net.liftweb._
import util._
import http._
import js._
import JsCmds._
import JE._
import scala.xml.NodeSeq

class Evently extends CometActor {

  // handle an incoming JSON event
  override def handleJson(in: Any): JsCmd = in match {
    case JsonCmd("pressed", _, key, _) => SetHtml("info", <b>You pressed {key}</b>)
    case _ => Noop
  }

  def render =
  <div>
  <span id="info"/> <!-- A place to put stuff -->
  {
    Script(jsonInCode) // include the JSON callback
  }
   <input type="text" onkeypress={jsonCall("pressed", JsRaw("event.which")).toJsCmd}/>
  </div>
}

So, the handleJson message gets called on the server whenever a key is pressed on the client (this example works in non-IE browsers, but that's just 'cause I'm using event.which rather than event.keyCode).

Hope this helps.

Thanks,

David



--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.




--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics

Rick R

unread,
Feb 24, 2010, 6:38:19 PM2/24/10
to lif...@googlegroups.com
That works beautifully, thanks.

With regards to using custom javascript functions, I have a function
processKeyPress. It's role is to filter key presses, only send events
to the server upon certain key presses.

I guess the easiest way to handle this would be to pass the function created by:


{jsonCall("pressed", JsRaw("event.which")).toJsCmd}

into processKeyPress so that it can invoke it if the event.which is
the correct type.

Unfortunately, I can't come up with a way to describe this in inline XML.

<input type="textarea" onkeypress="processKeyPress(event.which,
{jsonCall("pressed", JsRaw("event.which")).toJsCmd})"/>

leaving out the " causes a parse error.. adding the " cause it to be
evaulated as a string.

Any ideas?

Ross Mellgren

unread,
Feb 24, 2010, 6:41:23 PM2/24/10
to lif...@googlegroups.com
<input type="text" onkeypress={ "processKeyPress(event.which, " + jsonCall("pressed", JsRaw("event.which")).toJsCmd + ")" } />

-Ross

Rick R

unread,
Feb 24, 2010, 7:19:32 PM2/24/10
to lif...@googlegroups.com
That compiles, which is a great step. However, the resulting javascript is
onkeypress="processKeyPress(event.which, F379516302547JMS({'command':
&quot;pressed&quot;, 'params':event.which});)"

so it's attempting to execute the F37...
also, I don't know if the semicolon is allowed inside the parens.

Is there a way I can pass the function pointer separate from its params?

David Pollak

unread,
Feb 24, 2010, 10:57:40 PM2/24/10
to lif...@googlegroups.com
On Wed, Feb 24, 2010 at 3:38 PM, Rick R <rick.ri...@gmail.com> wrote:
That works beautifully, thanks.

With regards to using custom javascript functions,  I have a function
processKeyPress. It's role is to filter key presses, only send events
to the server upon certain key presses.

I guess the easiest way to handle this would be to pass the function created by:
 {jsonCall("pressed", JsRaw("event.which")).toJsCmd}

How about:

onkeypress={"processKeyPress(function(a) {"+jsonCall("pressed", JsRaw("a")).toJsCmd+"})"}

This will pass a function to the processKeyPress method.  If you want to send the message, you call the function with the value to send to the server.
 

Rick R

unread,
Feb 25, 2010, 11:43:00 AM2/25/10
to lif...@googlegroups.com
>>  {jsonCall("pressed", JsRaw("event.which")).toJsCmd}
>
> How about:
>
> onkeypress={"processKeyPress(function(a) {"+jsonCall("pressed",
> JsRaw("a")).toJsCmd+"})"}
>
> This will pass a function to the processKeyPress method.  If you want to
> send the message, you call the function with the value to send to the
> server.
>

Of course. The old wrap it in another function trick. I need to get more sleep.
Thanks for your help and patience on this.

David Pollak

unread,
Feb 25, 2010, 11:46:01 AM2/25/10
to lif...@googlegroups.com
On Thu, Feb 25, 2010 at 8:43 AM, Rick R <rick.ri...@gmail.com> wrote:
>>  {jsonCall("pressed", JsRaw("event.which")).toJsCmd}
>
> How about:
>
> onkeypress={"processKeyPress(function(a) {"+jsonCall("pressed",
> JsRaw("a")).toJsCmd+"})"}
>
> This will pass a function to the processKeyPress method.  If you want to
> send the message, you call the function with the value to send to the
> server.
>

Of course. The old wrap it in another function trick.

Yeah... the functional side of my brain was kicked into high gear when I found Scala... now the solution to *all* problems is "wrap it in a function". ;-)
 
I need to get more sleep.
Thanks for your help and patience on this.

--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.

Rick R

unread,
Feb 25, 2010, 11:54:40 AM2/25/10
to lif...@googlegroups.com
That's funny, because I'm a Haskell coder. As soon as I hit
Javascript, both sides of my brain turn off. I think it's the curly
braces that do it.
Reply all
Reply to author
Forward
0 new messages