使用Socket IO进行Jstree延迟加载

Jstree lazy loading using Socket IO

本文关键字:Jstree 延迟加载 进行 IO Socket 使用      更新时间:2023-09-26

我看到过使用jquery ajax的请求响应进行延迟加载,但没有任何方法可以使用Socket IO延迟加载节点数据吗?例如,我想要这样的

$("#tree").jstree({
      "core": {
          "data": function(data){
            socket.on("node",function(node){
              data = node;
            })
          }
       }
    });

每次单击时,我都可以调用数据回调来设置节点。对此有什么想法吗?期待中的感谢。

我不知道Socket IO,但我确信你用jstree做得不对:下面是你的函数应该是什么样子的:

function(currentNode, callback){
        //we got it called twice, prolly because of angular
        if(currentNode.id=='#'){
            var me = this;
            // perform a request with your framework here and call this once you have your data, '#' mean we're loading the root nodes
            var nodes = <your data>  
            callback.call(me, nodes);
        }else{
           var me = this;
           // we're loading child nodes
           // same as before ask the nodes to the server then call the callback with the data loaded.
           var nodes = <your data>
           callback.call(me, nodes)
       }    

}

注意:"this"是树实例。

这有帮助吗?

var ws = new WebSocket("ws://xxxx.xx/xxx/");
ws.onopen = function () {
    ws.onmessage = function (evt) {
        $("#codeeditor").jstree({
            "core": {
                "data": {
                    "data": function(data){
                         data = evt.data;
                    }
                }
            }
        });
    };
};

此外,还需要一些保持WebSocket始终连接的方法,我在这里没有展示。