JsInterop包装一个javascript函数属性 [英] JsInterop wrapping a javascript function property

查看:315
本文介绍了JsInterop包装一个javascript函数属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GWT 2.8,并且正在开发一个JavaScript库的包装器。

我试图包装的JavaScript类的一个属性是一个函数。我希望封装工作尽可能接近原生的JavaScript。我如何去包装一个JavaScript函数的JsProperty?

详细信息:
javascript类具有一个属性,该属性是在特定事件触发时调用的函数。我希望能够在Java中编写labmda函数,并将其分配给此属性,然后以javascript运行。

解决方案

正如亚当所说(在另一篇文章中更详细地解释),您可以用@JsFunction公开@JsProperty类型。

  @JsType(isNative = true)public class Foo {
@JsFunction public static interface BarFn {
Object invoke(Object ... args);
}
@JsProperty public BarFn bar;
}

我学习JsInterop的建议是探索其他项目,如: OpenLayers JsInterop wrapper Elemental2源代码探索github 。 Elemental2具有整个浏览器API,因此有大量示例,它是找到示例的好地方。 JsInterop文档此处


I am working with GWT 2.8, and I am working on a wrapper for a javascript library.

One of the properties of a javascript class I am trying to wrap is a function. I would like the wrapper to work as closely as possible to the native javascript. How do I go about wrapping a JsProperty that is a javascript function?

Details: The javascript class has a property that is a function that is called when a specific event is triggered. I would like to be able to write a labmda function in Java, and assign it to this property, which would then run in javascript.

解决方案

As Adam said (and explained in more detail in the other post), you can expose a @JsProperty with a @JsFunction type.

@JsType(isNative=true) public class Foo {
    @JsFunction public static interface BarFn {
        Object invoke(Object... args);
    }
    @JsProperty public BarFn bar;
}

My recommendation to learn JsInterop is to explore other projects like: OpenLayers JsInterop wrapper, Elemental2 source code, or explore github. Elemental2 has the whole browser API so there are plety of examples, it is a really good place to find examples. JsInterop documentation here.

这篇关于JsInterop包装一个javascript函数属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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