[英]Dojo tree - delay until server returns data
我有一个服务器函数,该函数生成代表文件系统一部分的JSON。
一旦用户从下拉列表中选择了一个项目,就会调用服务器功能。
到现在为止还挺好。
我的问题是,只有从服务器返回JSON数据时,才如何显示树? 由于我不是javascript专业人士,因此请尽量使您的回答冗长而完整!
var serverFunctionComplete = false;
var x = serverFunction();
while(!serverFunctionComplete) {
//just waiting
}
setTimeout(function() {
serverFunctionComplete = true;//if the server doesn't respond
}, 5000);
这应该使您入门。
您可以使用xhr对象中的sync : true
属性使ajax请求同步。 这将停止执行其他代码,直到从服务器收到响应并完成回调为止。
require(["dojo/request/xhr"], function(xhr){
xhr("example.json", {
handleAs: "json",
sync: true
}).then(function(data){
// Do something with the handled data
}, function(err){
// Handle the error condition
}, function(evt){
// Handle a progress event from the request if the
// browser supports XHR2
});
});
但是,这通常不是异步加载的最佳实践,因为异步加载是关于javascript和ajax的伟大事物之一。 建议您在xhr的回调函数中显示树,这样您的脚本就不会挂起轮询响应。
require(["dojo/request/xhr"], function(xhr){
xhr("example.json", {
handleAs: "json"
}).then(function(data){
// Display your Tree!
}, function(err){
// Handle the error condition
});
});
有关常规的异步线程管理,请参阅Dojo的Deffered类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.