4

How can I return a JavaScript function from JSNI in GWT? 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?

6
  • Why do you need to do that??? Commented Feb 5, 2013 at 16:29
  • The returned function is a d3.js transformation function which is used in several other JSNI methods to transform some data. To avoid redundancy and to dynamically change the transformation I would like to store it in a variable. Commented Feb 5, 2013 at 16:43
  • I don't understand why you cannot simply call this JS function from other JSNI methods, like you call any JS in your document, without storing it in a variable. Commented Feb 5, 2013 at 19:03
  • (1) why not call the function directly: as I said it is a transformation function from d3.js, but with custom parameters written by me. So whenever I would need the transformation function in any JSNI method I would have to write again the transformation function (duplicate code, maintainablility). (2) Therefore I define the function only once, store it in a variable and can then give this function as a parameter to any JSNI method which needs the transformation. Commented Feb 5, 2013 at 19:25
  • 1
    You can define your JS function, inject it in the document (using GWT ScriptInjector), and call it directly from any JSNI method. But I like Simon-Pierre's solution. Commented Feb 5, 2013 at 20:32

2 Answers 2

6

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);
Sign up to request clarification or add additional context in comments.

Comments

0

Lets consider you appName.nochache.js(GWT) in Homepage.html

in homepage.html

<script>
    function printMyName(name) {
        alert("Hello from JavaScript, " + name);
    }
    </script>

In your Gwt :

Within your Gwt source, you can access the sayHello() JS function through JSNI:

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...

1 Comment

I think extracting the relevant methods to the html page would also be a nice solution. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.