[英]GWT JSNI return a js-function
How can I return a JavaScript function from JSNI in GWT? 如何在GWT中从JSNI返回JavaScript 函数 ? I tried it the following way: 我尝试了以下方式:
/* JSNI method returning a js-function */
public static native JavaScriptObject native_getFunction() /*-{
return function(a,b){
//do some stuff with a,b
}
}-*/;
Store the function in a variable 将函数存储在变量中
/* outside from GWT: store the function in a variable */
JavaScriptObject myFunction = native_getFunction();
Using the function afterwards produces the following error message: 之后使用该函数会产生以下错误消息:
(TypeError): object is not a function
Does somebody know how to solve this problem? 有人知道如何解决这个问题吗?
This works for me. 这适合我。 Declare these methods: 声明这些方法:
public static native JavaScriptObject native_getFunction() /*-{
return function(a,b){
//do some stuff with a,b
}
}-*/;
private native void invoke(JavaScriptObject func)/*-{
func("a", "b");
}-*/;
And then, you use these methods this way: 然后,您以这种方式使用这些方法:
JavaScriptObject func = native_getFunction();
invoke(func);
Lets consider you appName.nochache.js(GWT)
in Homepage.html
让我们考虑您appName.nochache.js(GWT)
在Homepage.html
in homepage.html
在homepage.html
<script>
function printMyName(name) {
alert("Hello from JavaScript, " + name);
}
</script>
In your Gwt : 在你的Gwt中:
Within your Gwt source, you can access the sayHello() JS function through JSNI: 在你的Gwt源代码中,你可以通过JSNI访问sayHello()JS函数:
native void printMyNameInGwt(String name) /*-{
$wnd.printMyName(name); // $wnd is a JSNI synonym for 'window'
}-*/;
you can assign them to variables also 你也可以将它们分配给变量
native void printMyNameInGwt(String name) /*-{
var myname =$wnd.printMyName(name); // return that for your purposes
}-*/;
Note : if you are calling an js methods of any exterenal file that should be append on your html page with <script>
tags... 注意:如果您正在调用任何可以使用<script>
标签附加到html页面的exterenal文件的js方法...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.