On Wed, Jul 29, 2009 at 11:45 PM, Nolan Darilek <no...@thewordnerd.info>wrote:

>
> On 07/28/2009 07:28 PM, David Pollak wrote:
> > I'd do the REST API thing.  The mechanisms that Lift has for handling
> > API calls from the browser are numerous, but they are associated with
> > a session (you can do ajaxCall or a S.buildJsonFunc).
>
>
> Oh cool. When I listened to the podcast interview and heard about
> fast-pathing AJAX calls, I realized that this was exactly what I wanted,
> and that there wasn't a need to create a separate URL space just to
> accomodate this page update functionality--at least, not explicitly in
> the sense of what I meant by "API." Neat. Thanks for the method
> pointers, too, that helped me focus my reading a bit. So here's what I
> have. I have this JS code as an interface to the geolocation API:
>
> function Location() {
>
>   this.lat = 0;
>   this.lon = 0;
>
>   this.update = function(lat, lon) {
>     this.lat = lat;
>     this.lon = lon;
>     this.onUpdate(lat, lon);
>   };
>
>   this.onUpdate = function(lat, lon) {};
> }
>
> var loc = new Location();
>
> Next I wrote a snippet which needs to set loc.onUpdate to a function
> that calls into Lift to update the page. I have:
>
> class Geolocation {
>
>   def updatePosition(pos:String):JsCmd = Alert("Got an update: "+pos)
>
>   def update(in:NodeSeq):NodeSeq = <script type="text/javascript">
>     loc.onUpdate = function(lat, lon) {
>       {SHtml.ajaxCall(JsObj("lat" -> JsVar("lat"), "lon" ->
> JsVar("lon")), updatePosition _)._2}
>     };
> </script>
> }


Lift is XHTML.  The above will not work in XHTML because the code will be
escaped into XML.

Try the following:

def update(in: NodeSeq): NodeSeq = Script(JsRaw("""
loc.onUpdate = function(lat, lon) {
"""+SHtml.ajaxCall(JsObj("lat" -> JsVar("lat"), "lon" ->
JsVar("lon")), updatePosition _)._2.toJsCmd+
"""
};
"""))



>
>
> Two issues here. First, how do I get those braces around the JS function
> containing the ajaxCall into my HTML? Seems like there should be a way
> to escape braces so I can include them in NodeSeq, but \{ didn't seem to
> do it. I'm also quite new to JS as well, so perhaps there's a better way
> to set that callback. All I can come up with is changing the callback
> signature to accept an object rather than individual values so perhaps I
> can set it directly to the Lift-generated function, but I think I'd
> rather have raw values for individual position components for now.
>
> 2. Ideally, I'd like for the JsObj to be an actual Map[String, Float].
> Is there a way to do this? An included JSON parser that'd convert the
> string to a type I specify, perhaps? (assuming I'd have to give some
> sort of hint for Float vs. other numeric types, anyway)


I'm committing up jsonCall which augments ajaxCall by serializing
(JSON.stringify) the expression before sending it to the server and then
JSON parsing on the server before sending it to your function.




>
>
> Thanks. Wow, this really makes AJAX development something I'd actually
> enjoy doing. :)
>
> >
>


-- 
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 liftweb@googlegroups.com
To unsubscribe from this group, send email to 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to