开发者

JSTREE script execute asynchronously

开发者 https://www.devze.com 2023-04-05 14:48 出处:网络
I Have implemented JSTREE on my project everything work fine but recently I faced the issue which even though I manage to fix the concept of it is still unclear to me .

I Have implemented JSTREE on my project everything work fine but recently I faced the issue which even though I manage to fix the concept of it is still unclear to me .

Here What I meant ?

the JSTREE Code is Written in jquery document ready function Like this

$(document).ready(function() { 
  $("#someid").jstree( { 

    "json_data" : {
      "data" : [{
        "attr" : { "id" : "root" },
        "data" : {
          "title" : "Root",
          "attr" : { "id" : "root","href" : "#" ,"class" : "jstree-clicked" }
        },
        "children" : [
              {
              "attr" : {开发者_JAVA技巧"id" : "node-1"},
              "data" : {
                "title" : "Node-1",
                "attr" : {"id" : "node-1" ,"href": "#" }
              },
              "children": [

              ],
              "state" : "closed"
            },

              {
              "attr" : {"id" : "node-2"},
              "data" : {
                "title" : "Node-2",
                "attr" : {"id" : "node-2" ,"href": "#" }
              },
              "children": [

              ],
              "state" : "closed"
            },

              {
              "attr" : {"id" : "node-3"},
              "data" : {
                "title" : "Node-3",
                "attr" : {"id" : "node-3" ,"href": "#" }
              },
              "children": [

              ],
              "state" : "closed"
            },

              {
              "attr" : {"id" : "node-4"},
              "data" : {
                "title" : "Node-4",
                "attr" : {"id" : "node-4" ,"href": "#" }
              },
              "children": [

              ],
              "state" : "closed"
            },

              {
              "attr" : {"id" : "node-5"},
              "data" : {
                "title" : "Node-5",
                "attr" : {"id" : "node-5" ,"href": "#" }
              },
              "children": [

              ],
              "state" : "closed"
            }
     ],
     // the method where the ajax request will be sent to get thejson data to build the tree
    "ajax" : {
      "url" : "/my_url/tree",
      "data" : function (focus_element) {
        return { node : focus_element.attr ? focus_element.attr("id") : 0 };
      }
    }
  },

  "themes" : {
    "theme" : "classic",
    "dots" : true,
    "icons" : true
  },
  "plugins" : ["themes",  "json_data" ,"ui"]

})

There is a another jquery document ready function on the page which will click the anchor node created but the above jstree .

$(document).ready(function() {

 $("a#node-1").click()     

})

Now The problem come when the page is load the tree is constructed all the tree is constructed but the node-1 anchor link is not getting clicked .

Debugging it I found That the console.log always present me and empty array for a#node-1 which is very weird.

I put alert box in second document.ready function to see whether the tree is constructed when I'm requesting for the node. like this

$(document).ready(function() {

alert("JSTREE SHOULD BE CREATED BY NOW");

$("a#node-1").click()  

})

But to my astonishment the when the alert popped up JSTREE was still build the tree .

With some decent understanding of javascript I know that script tag on the page are synchronous in nature i.e the second script on the page execute only when the first execute

This make me believe is JSTREE running asynchronously .If yes Can please any one share some light on it .

By the Way I manage to achieve the same using jstree load handler

$("#someid").bind("loaded.jstree",

   function() {   
       $("a#node-1").click();   
       })


Your question has an interesting answer: I just found out that the code in jquery.jstree.js (jsTree 1.0-rc3), uses setTimer(...) to call .init() on the nodes, i.e. is asynchronous. I also used jsTree 1.0-rc1 in other project and it is synchronous.

I am afraid you won't find this documented as it is an implementation detail: it depends on the version you use.

As suggested by others, use the event mechanism in jstree to achieve what you want. On the other hand, Jurgen was right about the structure of the DOM.

(*) I browsed thru Github to see the original code, it seems the change happened in 2011. Before that you had:

jsTree 1.0-rc1 https://github.com/vakata/jstree/blob/0b8ebb2ba468b4c81e3c7645a9335584ddab41f5/jquery.jstree.js

line 59: instances[instance_id]._init();

whereas now,

jStree 1.0-rc3 https://github.com/vakata/jstree/blob/v.pre1.0/jquery.jstree.js

line 162: setTimeout(function() { instances[instance_id].init(); }, 0);


Through reading JSTree docs I found one interesting note: On page http://www.jstree.com/documentation/json_data in first code part noted that

// `state` and `children` are only used for NON-leaf nodes

But you trying to use them as nodes that has child nodes. Maybe this causes your issue.


Edit. Just checked the doc, my first guess was correct, removed the rest.

The id will be applied to an <li> which contains an <a>. Do

$("li#node-1 > a").click()

instead. Your CSS selector is wrong and will never match, this has nothing to do with async tree behavior.

Edit:

Now if your tree is contstructed async and you need to wait for its loaded event, you can do this with the following code before you initialize the tree, or you might lose the loaded event due to a timing race condition:

jQuery("#someid").bind("loaded.jstree", function (event, data) {
    $("li#node-1 > a").click();
    # or:
    $(this).jstree("open_node", "node-1");
});

If you just want to have the node opened on load, and not do anything complex otherwise, you can use the initially_open init parameter to set intial opened nodes too without fiddling with events at all.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号