Implement Javascript Function Callback with GWT JsInterop

走远了吗. 提交于 2019-12-04 04:48:45

问题


I want to wrap a javascript code like this :

map.addMarker({
        lat: -12.043333,
        lng: -77.028333,
        draggable: true,
        fences: [polygon],
        outside: function(m, f){
          alert('This marker has been moved outside of its fence');
        }
      });

Here how I write it in Java :

@JsType(namespace = JsPackage.GLOBAL, isNative = true, name = "Object")
public class MarkerOptions {
    @JsProperty
    public double lat;

    @JsProperty
    public double lng;

    @JsProperty
    public boolean draggable;

    @JsProperty
    public Polygon fences;

    @JsFunction
    public interface FunctionOutsideParam {
        void outside();
    }

    @JsProperty
    public FunctionOutsideParam outside;
}

But it's not working. Even thou it didn't have any error in my browser console. Anybody know how to make it working for the outside callback function? Thanks and regards.


回答1:


I finally found a solution. It appears that my java code was inconsistent with my javascript code. Thanks to Colin Alworth for pointing me the inconsistent part. So here is my full code :

@JsType(namespace = JsPackage.GLOBAL, isNative = true, name = "Object")
public class MarkerOptions {
    @JsProperty
    public double lat;

    @JsProperty
    public double lng;

    @JsProperty
    public boolean draggable;

    @JsProperty
    public Polygon[] fences;

    @JsFunction
    public interface FunctionOutsideParam {
        void outside(Marker m, Polygon[] f);
    }

    @JsProperty
    public FunctionOutsideParam outside;
}

Now whenever I run it, the outside function callback was invocated correctly. Thanks everyone. I hope my answer could help many others developer who tried to figured out how to implement js callback function with GWT JSInterop.



来源:https://stackoverflow.com/questions/44499407/implement-javascript-function-callback-with-gwt-jsinterop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!