Pass Java Callback Function to JSNI Method?
Asked Answered
N

1

5

I want to pass a success and a failure callback Java function to a JSNI method.

This is what I get so far but it does not work. How can I fix it?

package c;

public class A {

test(new Callback<String, String>() {

    @Override
    public void onFailure(String reason) {
        Window.alert("fail");
    }

    @Override
    public void onSuccess(String result) {
        Window.alert("suc");
    }
});


native void test(Callback<String, String> callback) /*-{

  var callback = $entry(function(event) {
     [email protected]::onSuccess(Ljava/lang/String;)("success!");
  });

}-*/;

}
Nous answered 5/12, 2013 at 18:37 Comment(0)
C
8

You can call the callback methods in this way:

native void test(Callback<String, String> callback) /*-{
  [email protected]::onSuccess(Ljava/lang/Object;)("success!");
}-*/;
Cecilla answered 6/12, 2013 at 8:9 Comment(3)
does this should work for a Callback<Integer,String> too? It gives me back a class cast exception when I try using the returned Integer converted to int with .intValue()Marquand
I found out for integer value of Callback<Integer,String> this should be used like: [email protected]::onSuccess(Ljava/lang/Object;)(@java.lang.Integer::valueOf(Ljava/lang/String;)("1"));Marquand
Will onFailure() ever be called in this case?Knecht

© 2022 - 2024 — McMap. All rights reserved.