Reputation: 2663
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;
});
}-*/;
Upvotes: 0
Views: 639
Reputation: 698
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.
Upvotes: 1