GWT JSNI - Java To Javascript Back To Java Results in undefined params -


i have racked brain on better part of 2 days. i've read through documentation on jsni here few different blog posts on jsni , passing variables this one , nothing indicates i'm doing wrong. i'm trying call gwt client side class javascript method i'm exporting javascript class loads. method takes params js method , stores them on instance of java class passed. seems work. but, once reference methods in java code, they're undefined. believe happening java class instance getting lost somehow after js finishes. here's code explain workflow...

i have java class named profilewidgee. class has method set local variables location, lattitude, , longitude. method name is...

public void handletargetpicked(string mloc, string mlat, string mlng)  {     loc = mloc.equalsignorecase("undefined") ? "" : mloc;     lat = mlat.equalsignorecase("undefined") ? "" : mlat;     lng = mlng.equalsignorecase("undefined") ? "" : mlng;     window.alert("setting on js side" + loc + lat + lng); } 

that method gets exported js function using jsni method called exportmyfunction...

public static native void exportmyfunction(profilewidgee instance)/*-{    $wnd.handletargetpicked = $entry(       instance.@com.n.j.client.widgees.profile.profilewidgee::handletargetpicked(ljava/lang/string;ljava/lang/string;ljava/lang/string;));  }-*/; 

that seems go fine. exports , i'm able call handletargetpicked in js follows...

handletargetpicked(encodeuricomponent(place.formatted_address),      encodeuricomponent(place.geometry.location.lat()),     encodeuricomponent(place.geometry.location.lng())); 

all of seems work , window.alert() displays correct values. leads me believe has appropriate instance of class , setting variables appropriately. later on though i'm in java class, try , reference variables , come 'undefined.'

window.alert("reading on java side" + pw.getloc() + pw.getlat() + pw.getlng()); 

this results in 'undefined' 3 values. big question ... possible set value in java class js side , use value later in class?

i ran similar situation , happened see post. couldn't see solutions suggested anywhere, tried debug myself.

what saw was, 'this' variable pointing window rather object instance.

so rather calling method directly e.g. handletargetpicked(arg1, arg2), used method.call() passing context e.g. handletargetpicked.call(instance, arg1, arg2). kind of approach solved issue me. hope helps.


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -