GWT JSNI 从 JSNI 函数返回字符串
GWT JSNI returning string from JSNI function
EDIT 我认为因为它是一个异步调用,所以当我调用方法数据时尚未设置。
String theData = getData("trainer") // not set yet
我有以下 JSNI 函数。如果我将此函数称为 returns 一个空字符串,但是它之前的 console.log 表明数据在那里。似乎由于某种原因无法返回数据。
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;
});
}-*/;
您认为是异步调用是正确的。
传递给 done 的回调的 return 未 return 编辑到您进行的原始调用。
如果您使用以下代码,您将在控制台中收到两条消息,第一条消息是空数据,第二条消息是正确数据。
String theData = getData("trainer");
consoleLog("The data is " + theData);
// suppose consoleLog as a native function to console.log
因此您可能应该像这样进行回调。
.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
})
doSomethingWith(theData) 甚至可以是对 Java 方法的调用。
EDIT 我认为因为它是一个异步调用,所以当我调用方法数据时尚未设置。
String theData = getData("trainer") // not set yet
我有以下 JSNI 函数。如果我将此函数称为 returns 一个空字符串,但是它之前的 console.log 表明数据在那里。似乎由于某种原因无法返回数据。
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;
});
}-*/;
您认为是异步调用是正确的。 传递给 done 的回调的 return 未 return 编辑到您进行的原始调用。
如果您使用以下代码,您将在控制台中收到两条消息,第一条消息是空数据,第二条消息是正确数据。
String theData = getData("trainer");
consoleLog("The data is " + theData);
// suppose consoleLog as a native function to console.log
因此您可能应该像这样进行回调。
.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
})
doSomethingWith(theData) 甚至可以是对 Java 方法的调用。