用GWT JsInterop实现Javascript函数回调 [英] Implement Javascript Function Callback with GWT JsInterop

查看:115
本文介绍了用GWT JsInterop实现Javascript函数回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  map.addMarker({
lat:-12.043333 ,
lng:-77.028333,
draggable:true,
fences:[polygon],
outside:function(m,f){
alert('This marker已经移出围栏');
}
});

在这里,我是如何使用Java编写的:

  @JsType(namespace = JsPackage.GLOBAL,isNative = true,name =Object)
public class MarkerOptions {
@JsProperty
public double土地增值税;

@JsProperty
public double lng;

@JsProperty
public boolean draggable;

@JsProperty
公共Polygon栅栏;

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

@JsProperty
public FunctionOutsideParam outside;
}

但它不起作用。即使你在我的浏览器控制台中没有任何错误。任何人都知道如何使它为外部回调函数工作?
感谢和问候。

解决方案

我终于找到了解决方案。看来我的java代码与我的javascript代码不一致。感谢科林阿尔沃思指出我不一致的部分。所以这里是我的完整代码:

  @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 []栅栏;

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

@JsProperty
public FunctionOutsideParam outside;
}

现在,无论何时运行它,外部函数回调都会正确调用。感谢大家。我希望我的回答能够帮助许多其他尝试了解如何使用GWT JSInterop实现js回调函数的开发人员。


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.

解决方案

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.

这篇关于用GWT JsInterop实现Javascript函数回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆