JSNI - Call Javascript native method from another

682 views
Skip to first unread message

Jésica

unread,
Sep 14, 2011, 12:57:42 PM9/14/11
to Google Web Toolkit
Hi, I'm facing problems for calling a native method from another. i'm
trying to do It like this:

//JSNI code
public native void dragStart()/*-{
alert("OnDragStart");
}-*/;

public native void addPushpin()/*-{
(...)
blah.Events.addHandler(pin, 'dragstart', dragStart);
(...)
}-*/

I'm getting an error saying "dragStart" is not defined.
Is there an special way for making this interaction work?

Thanks,
Jésica.



dreamer

unread,
Sep 15, 2011, 6:34:07 AM9/15/11
to Google Web Toolkit
Technically, Here you are trying to call java method from javascript.
I did not come across this situation, try full compile if throws any
errors.

Global School District
http://schoolk12.appspot.com/

Thomas Broyer

unread,
Sep 15, 2011, 6:40:27 AM9/15/11
to google-we...@googlegroups.com


On Wednesday, September 14, 2011 6:57:42 PM UTC+2, Jésica wrote:
Hi, I'm facing problems for calling a native method from another. i'm
trying to do It like this:

//JSNI code
public native void dragStart()/*-{
     alert("OnDragStart");
}-*/;

public native void addPushpin()/*-{
    (...)
    blah.Events.addHandler(pin, 'dragstart', dragStart);

This should work:

var that = this;
blah.Events.addHandler(pin, 'dragstart', $entry(function() {
   that.@path.to.YourClass::dragStart()();
}));

($entry() is so that exceptions throw by the wrapped function, thus by your dragStart method, which the wrapped function calls, will go through GWT.UncaughtExceptionHandler; it also ensures Scheduler#scheduleEntry and Scheduler#scheduleFinally commands will be run around that call back to Java code)

Alexandre Dupriez

unread,
Sep 15, 2011, 10:14:44 AM9/15/11
to Google Web Toolkit
The point is that GWT compiler will not understand the "dragstart"
string in your addHandler(.) method, and let it untouched. Once your
unit will be compiled, the Javascript piece of code you wrote will
refer to an in-existing method, since the actual dragstart() method
will be given another binding name by GWT (eventually obfuscated).

Alexandre

Jésica

unread,
Sep 15, 2011, 4:43:17 PM9/15/11
to Google Web Toolkit
Thomas, your example with $entry worked perfectly. I have a question
about your code snippet: which is representing *that* var assignment
in this case and why Is It necessary to use It?.
Thanks,
Jésica.

On Sep 15, 7:40 am, Thomas Broyer <t.bro...@gmail.com> wrote:
> On Wednesday, September 14, 2011 6:57:42 PM UTC+2, Jésica wrote:
>
> > Hi, I'm facing problems for calling a native method from another. i'm
> > trying to do It like this:
>
> > //JSNI code
> > public native void dragStart()/*-{
> >      alert("OnDragStart");
> > }-*/;
>
> > public native void addPushpin()/*-{
> >     (...)
> >     blah.Events.addHandler(pin, 'dragstart', dragStart);
>
> This should work:
>
> var that = this;
> blah.Events.addHandler(pin, 'dragstart', $entry(function() {
>    th...@path.to.YourClass::dragStart()();

Thomas Broyer

unread,
Sep 16, 2011, 4:43:40 AM9/16/11
to google-we...@googlegroups.com
'this' is a keyword, which depends on the way the function has been called. Because you do not pass "this" as an argument to addHandler, it's clear that the function you pass won't ever be called with its "this" set to the value of "this" at the time you called addHandler. Storing "this" in to a variable works around it.

in Java, this is a bit similar to the "this" in anonymous classes refering to the anonymous class, and not the class it was defined in. However, in Java, you have the TheParentClass.this notation; that does not exist in JavaScript (and, "this" is contextual to the *call* in JS, whereas it's contextual to the *declaration* in Java; always).

Jésica

unread,
Sep 16, 2011, 10:21:50 AM9/16/11
to google-we...@googlegroups.com
Crystal clear!, Thanks everyone.
Jésica.
Reply all
Reply to author
Forward
0 new messages