我想包装这样的javascript代码:
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'); } });
这是我如何用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; }
但这不起作用。即使您在我的浏览器控制台中也没有任何错误。有人知道如何使它适用于外部回调函数吗?感谢致敬。
我终于找到了解决方案。看来我的Java代码与我的JavaScript代码不一致。感谢Colin Alworth为我指出了不一致的部分。所以这是我的完整代码:
@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; }
现在,每当我运行它时,都会正确调用外部函数回调。感谢大家。我希望我的回答能对其他尝试弄清楚如何使用GWT JSInterop实现js回调函数的开发人员有所帮助。