[英]JavaScript callback function seems to work, but does not draw the data
I've already been through many tutorials on callbacks (eg here , on SO and this gem ) 我已经看过很多关于回调的教程(例如, 此处 , SO和此gem )
By far the best one was this gem . 到目前为止,最好的是这种宝石 。 Especially this part helped me to understand.
特别是这部分帮助我理解了。
function addOne(thenRunThisFunction) {
waitAMinuteAsync(function waitedAMinute() {
thenRunThisFunction()
})
}
addOne(function thisGetsRunAfterAddOneFinishes() {})
So I implemented it as described there. 因此,我按照那里的描述实施了它。 Now everything seems to work on console output, but it does not render my d3 force directed graph as expected.
现在,一切似乎都可以在控制台输出上运行,但是它并不能按预期方式呈现我的d3强制定向图 。
[some fancy variables used by force graph]
function draw(json) {
java.alert(json);
[awsome force graph stuff]
};
var data = undefined;
function readInputStream(callback){
data = java.getJSONObject();
if(!data == ""){
java.alert("success");
} else {
java.alert("failure");
}
callback();
}
function prepareDraw() {
java.alert("prepare called");
draw(data);
}
readInputStream(prepareDraw);
Please note that java
is an identifier. 请注意,
java
是一个标识符。 I'm running it all in a JavaFX WebView and all methods after java.
我在JavaFX WebView和
java.
之后的所有方法中全部运行java.
are actual java methods I use in JavaScript. 是我在JavaScript中使用的实际Java方法。 See this:
看到这个:
public class JStoFXBridge {
private String json;
public String getJSONObject(){
return json;
}
public void setJSONObject(String string){
this.json = string;
}
public void alert(String alert){
System.out.println("Alert: " + alert);
}
}
Now my console prints: 现在,我的控制台将打印:
Alert: success
Alert: prepare called
Alert: {"nodes":[{"name":"sepp","id":0,"group":2},{"name":"hans","id":1,"group":6}, [...]
Which is the right format for my JSON String and which is (as far as I see it) in the right order to be executet. 哪种是我的JSON字符串的正确格式,并且(据我所知)以正确的顺序执行。
data
gets filled and is not empty. data
被填充且不为空。 the callback method
gets called. the callback method
被调用。 My draw function
has a filled & valid
JSON String. 我的
draw function
具有一个已filled & valid
JSON字符串。 What is going wrong here? 这是怎么了? I have no explination why this won't work.
我不知道为什么这行不通。
Sometimes it's easier than you think... 有时候比您想像的要容易...
java.alert("prepare called");
var json = JSON.parse(data);
draw(json);
Problem: data
was a String, d3 wants a JSON Object. 问题:
data
是一个字符串,d3需要一个JSON对象。 Watch here . 在这里观看 。 New output:
新的输出:
Alert: success
Alert: prepare called
Alert: [object Object]
Now renders the graph just fine. 现在将图形渲染得很好。
var data = {"nodes":[{"name":"sepp", [...]
works because JavaScript interpretes it as a JSON Object. var data = {"nodes":[{"name":"sepp", [...]
起作用是因为JavaScript将其解释为JSON对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.