Call a scala function through Javascript

916 views
Skip to first unread message

steve

unread,
Dec 30, 2011, 12:30:49 PM12/30/11
to Lift
Hey guys,
Two parter coming up...

A) So calling a js function through lift is simple enough using
JE.Call. Is there a way to do the reverse? In a js function (in a
separate .js file) call access code (classes, methods, variables) from
my scala files? I hope this question makes sense. I've been
searching around and can't seem to figure it out.

B) I have a js function that listens for a click on a DOM element and
part of the response is to update a Shtml.ajaxSelect dropdown. This
works fine, but while the Select box does get updated, the ajax
function is not being executed.

I'll explain anything more if necessary. I hope someone can shed some
light on this for me. I appreciate any advice. I also wasn't sure if
I should post this as two posts or not, but their basically related,
so i figured one should be good.

Thanks guys!

David Pollak

unread,
Dec 30, 2011, 1:24:45 PM12/30/11
to lif...@googlegroups.com
On Fri, Dec 30, 2011 at 9:30 AM, steve <sgio...@manaproducts.com> wrote:
Hey guys,
 Two parter coming up...

A) So calling a js function through lift is simple enough using
JE.Call.  Is there a way to do the reverse?  In a js function (in a
separate .js file) call access code (classes, methods, variables) from
my scala files?  I hope this question makes sense.  I've been
searching around and can't seem to figure it out.


You can't execute code in an arbitrary Scala class from JavaScript.  What I mean by that is you cannot do: myScalaObject.setName("foo"); myScalaObject.save();

But, you can use SHtml.ajaxCall() or SHtml.jsonCall() to marshall calls back to the server, but you have to write your own Scala functions on the server to handle the messages.
 
B) I have a js function that listens for a click on a DOM element and
part of the response is to update a Shtml.ajaxSelect dropdown.  This
works fine, but while the Select box does get updated, the ajax
function is not being executed.

Please put together example code (see https://www.assembla.com/wiki/show/liftweb/Posting_example_code ) so we can all see the issue and the code.
 

I'll explain anything more if necessary.  I hope someone can shed some
light on this for me.  I appreciate any advice.  I also wasn't sure if
I should post this as two posts or not, but their basically related,
so i figured one should be good.

Thanks guys!

--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code



--
Visi.Pro, Cloud Computing for the Rest of Us http://visi.pro
Lift, the simply functional web framework http://liftweb.net


dbcfd

unread,
Dec 30, 2011, 1:43:11 PM12/30/11
to Lift
On Dec 30, 12:30 pm, steve <sgiord...@manaproducts.com> wrote:
> B) I have a js function that listens for a click on a DOM element and
> part of the response is to update a Shtml.ajaxSelect dropdown.  This
> works fine, but while the Select box does get updated, the ajax
> function is not being executed.

Ajax select takes a function, so you don't need to use other Js
functions for monitoring. Simply use SetHtml in your callback function
for SHtml.ajaxSelect to update the dropdown.

steve

unread,
Dec 30, 2011, 2:01:52 PM12/30/11
to Lift
Thanks for both of your responses. I'm going to look more closely at
AjaxCall.

@dbcfd: In my case I have an element(happens to be a span, but could
be an img) and I have a js file that listens for clicks on this
element. When you click on a specific span, I update the ajaxSelect
which is built in a lift snippet. I use basic JQuery to update the
select value. This works fine. But when the Select changes, it
doesn't also run the Ajax function associated with it. This function
only seems to run when the Select menu itself it manipulated itself by
the user. If this isn't clear, I can try to make a quick simple
example and add it to git, but I'd rather not if this makes sense to
you.

-Steve

dbcfd

unread,
Dec 30, 2011, 2:22:25 PM12/30/11
to Lift
Bind the click on that element to an SHtml.jsonCall.

When the user clicks, it will invoke a snippet function, which can
update the select using sethtml, and call any other snippet functions.

I use this type of functionality for updating comet actors via a comet
server, when a user clicks a canvas element.

Jq("#my_canvas") ~>
JsFunc("click",
AnonFunc("evt",
SHtml.jsonCall(JsRaw("getJsonFromClickEvt(evt)"), handleJsonClickEvent
_)._2)
)

Diego Medina

unread,
Dec 30, 2011, 2:25:27 PM12/30/11
to lif...@googlegroups.com

You can see a working example using jsoncall on this post
http://blog.fmpwizard.com/back-button-and-bookmark-meet-lift-comet-revi

There is even a live app that shows how it works.

Hope it helps

Diego
Sent from my android cell

Diego Medina

unread,
Dec 30, 2011, 2:29:31 PM12/30/11
to lif...@googlegroups.com

For B, you may find reactive web useful. See this other post that shows something similar to what you want.
http://blog.fmpwizard.com/scala-reactive-web-lift-great-user-experience

Diego
Sent from my android cell

On Dec 30, 2011 12:31 PM, "steve" <sgio...@manaproducts.com> wrote:
Reply all
Reply to author
Forward
0 new messages