0

EDIT I think because it is an asychronous call that when I call the method data has not been set yet.

String theData = getData("trainer") // not set yet

I have the following JSNI function. The if I call this function it returns an empty string, however the console.log before it show that data is there. Seems data cannot be returned for some reason.

public native String getData(String trainerName)/*-{
    var self = this;
    $wnd.$.get( "http://testdastuff.dev/trainerstats", { trainer: trainerName} )
     .fail(function() {
         $wnd.console.log("error");
      })
     .done(function( data ) {
         console.log("DATA IS: " + data);
         return data;
    });

    }-*/;

1 Answer 1

1

Your thought that it is a asynchronous call is correct. The return of the callback passed to done is not returned to the original call you made.

If you used the following code, you'll get 2 messages in the console, in the first you'll get empty data, and in the second, the correct data.

String theData = getData("trainer");
consoleLog("The data is " + theData);
// suppose consoleLog as a native function to console.log

Thus you should probably do your callback like this.

.done(function( data ) {
     console.log("DATA IS: " + data);
     theData = data; // if theData is within the same scope and you want to store
     doSomethingWith(theData); // <-- here you can interact with theData
})

The doSomethingWith(theData) could even be an invocation to a Java method.

Sign up to request clarification or add additional context in comments.

Comments

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.