On Sun, Aug 23, 2009 at 10:40 PM, jack <[email protected]> wrote:
>
> I am trying to make a simple ajax form. The code is below. The
> Javascript error I am getting when I hit the send button is:
>
> uncaught exception ReferenceError: F777221462447MPT is not defined
>
> Am I missing something?
>
> <lift:surround with="default" at="content">
> <h1>Hello FormAjax</h1>
> <lift:HelloFormAjax.show>
> Hello <hello:who/><br/>
> <label for="whoField">Who :</label><hello:whoField/>
> <hello:submit/>
> </lift:HelloFormAjax.show>
> </lift:surround>
>
> create src/main/scala/sandbox/lift/hellodarwin/snippet/HelloFormAjax
> package sandbox.lift.hellodarwin.snippet
>
> import scala.xml.NodeSeq
> import net.liftweb.http.S._
> import net.liftweb.http.SHtml._
> import net.liftweb.util.Helpers._
> import net.liftweb.http.js.{JsCmd, JsCmds}
>
> class HelloFormAjax {
> def whoNode(str: String) = <span id="who">{str}</span>
>
> def updateWho(str: String): JsCmd = {
> println("updateWho on " + str)
> JsCmds.Run("$('#who').text('"+str+"')")
This will fail if str contains any non-printables or "'". Please use:
str.encJs
To encode a Scala String into a JavaScript friendly String.
But better, you could use:
SetHtml("who", scala.xml.Text(str))
>
> }
>
>
> def show(xhtml: NodeSeq): NodeSeq = {
> bind("hello", xhtml,
> "whoField" -> text("world", null) % ("size" -> "10") % ("id" -
> > "whoField"),
> "submit" -> <button type="button">{?("Send")}</button> %
> ("onclick" -> ajaxCall(JsRaw("$('#whoField').attr('value')"), s =>
> updateWho(s))),
ajaxCall returns a tuple of (String, JsExp). I'm not sure why the compiler
is letting that get converted into the second half of a pair for
UnparsedAttribute purposes... but... in order to use it, you must do:
ajaxCall(JsRaw("$('#whoField').attr('value')"), s =>updateWho(s))._2
But... it'd be better to ValById:
ajaxCall(ValById("whoField"), , s =>updateWho(s))._2
I've added a few helpers to ajaxButton, so you can do:
"submit" -> ajaxButton(Text(?("Send")), ValById("whoField"), s =>
updateWho(s))
>
> "who" -> whoNode("world")
> )
> }
> }
>
> >
>
--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Lift" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/liftweb?hl=en
-~----------~----~----~----~------~----~------~--~---