How can I tell if jsTree has fully loaded?

javascript, jstree

Solution

I used setInterval and clearInterval:

var interval_id = setInterval(function(){
     // $("li#"+id).length will be zero until the node is loaded
     if($("li#"+id).length != 0){
         // "exit" the interval loop with clearInterval command
         clearInterval(interval_id)
         // since the node is loaded, now we can open it without an error
         $("#tree").jstree("open_node", $("li#"+id))
      }
}, 5);

JStree's ".loaded" callback only works for root nodes; "._is_loaded" may work instead of checking the node's length, but I haven't tried it. Either way, the animation settings cause nodes that are deeper in the tree to be loaded a few milliseconds later. The setInterval command creates a timed loop that exits when your desired node(s) is loaded.

Problem

I am trying to write a function that opens specific nodes on a jsTree but I am having a problem where the function is executed before my base tree is loaded from the ajax call. How can I tell if my jstree data has been loaded and wait until it is done loading. Below is the function I am trying to use. ``` function openNodes(tree, nodes) { for (i in nodes) { $('#navigation').jstree("open_node", $(nodes[i])); } } ``` I am loading my initial tree with the following command ``` $("#navigation").jstree({ "json_data": { "ajax": { "url": function(node) { var url; if (node == -1) { url = "@Url.Action("BaseTreeItems", "Part")"; } else { url = node.attr('ajax'); } return url; }, "dataType": "text json", "contentType": "application/json charset=utf-8", "data": function(n) { return { id: n.attr ? n.attr("id") : 0, ajax: n.attr ? n.attr("ajax") : 0 }; }, "success": function() { } } }, "themes": { "theme": "classic" }, "plugins": ["themes", "json_data", "ui"] }); ```

Original source