JSNI dynamic function reference in GWT

萝らか妹 提交于 2019-12-01 08:04:38

问题


I would like to call arbitrary js function from gwt. Function name would be inside functionname variable. Something like this:

private static native String execute(String functionName, JavaScriptObject data) /*-{
    return $wnd.functionName(data);
}-*/;

I assume that something like this could be possible, but how to create fn variable to represent my arbitrary functionname function.

private static native String execute(JavaScriptObject fn, JavaScriptObject data) /*-{
    return fn(data);
}-*/;

回答1:


If you need to invoke function by name, you need to do something like this:

private static native String execute(String functionName,JavaScriptObject data)/*-{
     $wnd[functionName](data);

}-*/;

To get reference to a function you will need to use JSNI like this:

private static native JavaScriptObject getFunction(String functionName)/*-{
    return  $wnd[functionName];

}-*/;


来源:https://stackoverflow.com/questions/8987438/jsni-dynamic-function-reference-in-gwt

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