mbraak / jqTree

Tree widget for jQuery
https://mbraak.github.io/jqTree/
Apache License 2.0
1.02k stars 177 forks source link

Duplicate event call after re-initalization #241

Closed jonaswindey closed 10 years ago

jonaswindey commented 10 years ago

I'm having an issue with events that are triggered multiple times when a tree is initliazed multiple times.

function createTree(data) { $('#tree').tree({ data: data, dragAndDrop: true, autoOpen: true, }); }

$('#tree').bind( 'tree.select', function(event) { // some ajax and logic here }

This works perfect the first time. However, in my application, I call createTree multiple times (when data has been updated externally from an ajax call). This triggers multiple times the 'tree.select' event, which is not the expected behaviour, since it calls multiple times my logic (and servercalls)

Ofcourse I could set a variable isCalled and check that on each event, but that sounds dirty.

I've tried using $('#tree').unbind('tree.select'), but that doesn't change.

I also read that jquery's bind() method has been deprecated and replaced by on(), but that gives the same effect (jqTree's docs say to use bind() though)

Any suggestions? Thanks

mbraak commented 10 years ago

It's better to call the initialization function only once. You can load new data in the tree using the loadData method.

$('#tree').tree('loadData', data);

I hope this works.

mbraak commented 10 years ago

Fixed in the dev branch: if you initialize the tree multiple times, then the events must not be trigger multiple times.