如何在dynatree上强制执行同步模式? [英] How can I force sync mode on dynatree?
问题描述
我正在使用Martin Wendt的 Dynatree ,并希望在Ajax中动态,动态地更新树递归地.
I'm using Martin Wendt's Dynatree and want to update the tree in Ajax, dynamically and recursively.
为此,我正在使用tree.getNodeByKey(nodes[i-1]).appendAjax({});
之类的函数
和tree.activateKey(nodes[i]);
在for
循环中.
For this, I'm using functions like tree.getNodeByKey(nodes[i-1]).appendAjax({});
and tree.activateKey(nodes[i]);
in a for
loop.
将alert("Hello world");
添加到循环中时,我的代码可以在95%的时间内正常工作.没有它,代码将永远无法工作.
When adding alert("Hello world");
into the loop, my code works correctly 95% or the time. Without it, the code never works.
问题显然来自于 Dynatree正在以异步模式执行其Ajax请求,并且在添加到.initAjax
和/或.appendAjax
时似乎忽略了async:false
:
The problem is obviously coming from the fact that Dynatree is performing its Ajax requests in asynchronous mode, and seem ignoring the async:false
, when added to .initAjax
and/or .appendAjax
:
jQuery('#tree').dynatree("getTree").getNodeByKey(nodes[i-1]).appendAjax({
type: 'POST',
url: "inc/treeNodes.php",
dataType: 'json',
data: {key: nodes[i]},
async:false
});
我尝试在对.appendAjax
的调用周围添加setTimeout (function() {},2000);
,但是它阻止了此方法的工作.
I tried adding setTimeout (function() {},2000);
around the call to .appendAjax
, but it prevented this method to work.
可以将Dynatree修改为支持异步模式吗?
Could Dynatree be modified to support asynchronous mode?
推荐答案
尽管它不能真正回答在同步模式下运行Dynatree的问题,但我发现了一个解决方法,它几乎可以模拟Ajax同步行为
Although it does not truly answer the question of running Dynatree in synchronous mode, I found a workaround that almost emulates an Ajax synchronous behavior.
以下变通办法似乎在localhost上可以正常运行,但是如果站点是远程的则不能.
the following workaround seems working reasonably well on localhost, but not if the site is remote.
在对Dynatree方法进行重要调用之前,请添加一个setTimeOut()调用,最后将一些内容写入控制台.
Before important calls to Dynatree's method, add a setTimeOut() call at the end of which something will be written to the console.
for (i=1; i < n; i++) {
(...)
setTimeout(function(){console.log('_')}, 3000);
jQuery('#tree').dynatree("getTree").getNodeByKey(nodes[i-1]).appendAjax({...});
(...)
}
setTimeout
不会像延迟所建议的那样使代码变慢.
例如,如果循环具有三个迭代,则不必等待9秒(3x 3000)就可以完成所有三个appendAjax调用所完成的工作.
我假定.appendAjax()
调用是在setTimeout
仍在运行的同时执行的,但是后面的调用似乎对文档窗口产生了神奇的效果.
The setTimeout
does not slower the code as much as the delay would suggest.
For instance, if the loop has three iterations, you don't have to wait 9 seconds (3x 3000) before the work done by all three appendAjax calls complete.
I assume that .appendAjax()
calls are performed whilst setTimeout
is still running, but the later seem having some magic effect on the document window.
使用setTimeout
和触发器console.log()
与插入alert();
调用具有相似的效果,但是对于访问者来说是无声的".
Using setTimeout
and trigger console.log()
has similar effect as inserting alert();
calls, but is "silent" for the visitor.
这篇关于如何在dynatree上强制执行同步模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!