django
django

Reputation: 153

How to call JSNI method from JSNI method in GWT

Is it possible to call native method from another native method in the same class in this way?

public native JavaScriptObject mySECONDJsniMethod(String name) /*-{
        //..
        return secondVar;
}-*/;

public native JavaScriptObject myFIRSTJsniMethod(String name) /*-{
        var secondVar = mySECONDJsniMethod(name);
        //..
        return firstVar;
}-*/;

In my Chrome Console it returns: Uncaught TypeError: mySECONDJsniMethod is not a function . Any ideas? Thank you.

Upvotes: 1

Views: 1358

Answers (1)

Daniel Trebbien
Daniel Trebbien

Reputation: 39228

Yes. Within the JSNI for myFIRSTJsniMethod(), use the normal JSNI syntax for calling a Java method.

For example, if these two methods are in the class com.mycompany.Test, you could call mySECONDJsniMethod() like so:

public native JavaScriptObject myFIRSTJsniMethod(String name) /*-{
    var secondVar = [email protected]::mySECONDJsniMethod(Ljava/lang/String;)(name);
    //..
    return firstVar;
}-*/;

See the GWT documentation on Accessing Java Methods and Fields from JavaScript for more information.

EDIT: Here is a complete compilable example:

package com.mycompany.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.JavaScriptObject;

public class SO26277049 implements EntryPoint {

    @Override
    public void onModuleLoad() {
        final JavaScriptObject firstVar = myFIRSTJsniMethod("hello world!");
    }

    public native JavaScriptObject mySECONDJsniMethod(String name) /*-{
        var secondVar = {
            name: name
        };
        return secondVar;
    }-*/;

    public native JavaScriptObject myFIRSTJsniMethod(String name) /*-{
        var secondVar = [email protected]::mySECONDJsniMethod(Ljava/lang/String;)(name);
        $wnd.alert(secondVar.name);
        var firstVar = secondVar;
        return firstVar;
    }-*/;
}

Upvotes: 1

Related Questions