How can I force sync mode on dynatree?

て烟熏妆下的殇ゞ 提交于 2019-12-11 14:48:26

问题


I'm using Martin Wendt's Dynatree and want to update the tree in Ajax, dynamically and recursively.

For this, I'm using functions like tree.getNodeByKey(nodes[i-1]).appendAjax({}); and tree.activateKey(nodes[i]); in a for loop.

When adding alert("Hello world"); into the loop, my code works correctly 95% or the time. Without it, the code never works.

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
        });

I tried adding setTimeout (function() {},2000); around the call to .appendAjax, but it prevented this method to work.

Could Dynatree be modified to support asynchronous mode?


回答1:


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.

Edit: the following workaround seems working reasonably well on localhost, but not if the site is remote.


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({...});
        (...)
    }

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.

Using setTimeout and trigger console.log() has similar effect as inserting alert(); calls, but is "silent" for the visitor.



来源:https://stackoverflow.com/questions/49831515/how-can-i-force-sync-mode-on-dynatree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!