ryan
ryan

Reputation: 33

jstree select multiple nodes fire one event

I have a tree of files and am trying to create an editor for. When one node is selected a panel is loaded for that specific node. If multiple nodes are selected a more generic panel be loaded for all selected nodes.

The problem is, when selecting multiple nodes the "select_node.jstree" is fired for each node.

a snippet of related code...

$("#tree).on("select_node.jstree", function(event, node) {
    var selected = node.instance.get_selected();
    if(selected.length === 1) {
        $("#editor").load(url);
    } else if(selected.length > 1) {
        $.post(url, {
            data: selected
        }, function(res) {      
            $("#editor").html(res);
        });
    }
});

So... with this if I select 5 items I am doing 1 GET and 4 POSTS.

What I am looking for is 1 GET (the first node selected) and 1 POST (the collection of selected nodes)...

Would it be just a timeout? I feel like I am missing something obvious. I am far from a good programmer, so any direction would be appreciated.

Upvotes: 0

Views: 2153

Answers (1)

ryan
ryan

Reputation: 33

So, I was able to use doTimeout library to manage this. It works, not sure if it is optimal.

https://github.com/cowboy/jquery-dotimeout

$('#tree').on("select_node.jstree", function(event, node) {
    $.doTimeout('select', 500, function () {
        var selected = node.instance.get_selected();
        if(selected.length === 1) {
            $('#editor').load(url);
        } else if(selected.length > 1) {
           $.post(url, {
                data: selected
            }, function(res) {      
                $('#editor').html(res);
            });
        }
    });
});

Upvotes: 1

Related Questions