简体   繁体   English

GWT JSNI-Java到Java返回Java导致未定义的参数

[英]GWT JSNI - Java To Javascript Back To Java Results in undefined params

I have racked my brain on this for the better part of two days. 在两天的大部分时间里,我已经全力以赴。 I've read through the documentation on JSNI here as well as a few different blog posts on JSNI and passing variables like this one and nothing indicates I'm doing anything wrong. 我已经通过在JSNI的文档阅读这里以及在JSNI几个不同的博客文章和传递变量像这样一个并没有什么表示,我做错什么。 Essentially what I'm trying to do is call from my GWT Client Side class to a javascript method which I'm exporting to javascript as my class loads. 本质上,我想做的是从GWT客户端类调用javascript方法,并在类加载时将其导出到javascript。 That method takes params from another JS method and stores them on the instance of the Java class that I passed. 该方法从另一个JS方法获取参数,并将它们存储在我传递的Java类的实例上。 That seems to work. 这似乎有效。 But, once I reference those methods back in my java code, they're undefined. 但是,一旦我在Java代码中引用了这些方法,它们就是未定义的。 I believe that what is happening is that my Java Class instance is getting lost somehow after the JS finishes. 我相信正在发生的事情是我的Java类实例在JS完成之后以某种方式丢失了。 Here's some code to help explain the workflow... 这是一些代码来帮助解释工作流程...

I have a Java Class named ProfileWidgee. 我有一个名为ProfileWidgee的Java类。 That class has a method to set local variables for location, lattitude, and longitude. 该类具有一种为位置,纬度和经度设置局部变量的方法。 That 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 to the JS as a function using a JSNI method called exportMyFunction... 该方法使用称为exportMyFunction的JSNI方法作为函数导出到JS ...

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 all seems to go fine. 一切似乎都很好。 It exports and I'm able to call the handleTargetPicked in my JS as follows... 它导出,我可以按如下方式在我的JS中调用handleTargetPicked ...

handleTargetPicked(encodeURIComponent(place.formatted_address), 
    encodeURIComponent(place.geometry.location.lat()),
    encodeURIComponent(place.geometry.location.lng()));

All of this seems to work and the Window.alert() displays the correct values. 所有这些似乎都起作用,并且Window.alert()显示正确的值。 That leads me to believe that it has the appropriate instance of my class and that it is setting the variables appropriately. 这使我相信它具有类的适当实例,并且正在适当地设置变量。 Later on though as I'm back in my Java class, I try and reference those variables and they always come back as 'undefined.' 稍后,尽管当我回到Java类中时,我尝试引用这些变量,但它们始终以“未定义”的形式返回。

Window.alert("reading on the java side" + pw.getLoc() + pw.getLat() + pw.getLng());

This results in 'undefined' for all three values. 这将导致所有三个值均为“未定义”。 So my big question is ... is it possible to set a value in your Java class from the JS side and then use that value later in your class? 所以我的大问题是...是否可以从JS端在Java类中设置一个值,然后稍后在您的类中使用该值?

I just ran into a somewhat similar situation like this and happened to see your post. 我遇到了类似的情况,碰巧看到了您的帖子。 I couldn't see any solutions suggested anywhere, so tried to debug it myself. 我在任何地方都看不到任何建议的解决方案,因此请尝试自行调试。

What I saw was, 'this' variable was pointing to Window rather than the object instance. 我看到的是,“此”变量指向的是Window而不是对象实例。

So rather than calling the method directly like eg handleTargetPicked(arg1, arg2), I used method.call() passing the context like eg handleTargetPicked.call(instance, arg1, arg2). 因此,与其使用像handleTargetPicked(arg1,arg2)这样的直接调用方法,不如我使用method.call()传递了诸如handleTargetPicked.call(instance,arg1,arg2)之类的上下文。 That kind of approach solved the issue for me. 这种方法为我解决了这个问题。 Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM