JSNI dynamic function reference in GWT

2019-02-19 17:13发布

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);
}-*/;

标签: gwt jsni
1条回答
对你真心纯属浪费
2楼-- · 2019-02-19 17:41

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];

}-*/;
查看更多
登录 后发表回答