mbraak / jqTree

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

tree.init only fired when using dataUrl #323

Closed m3rls closed 10 years ago

m3rls commented 10 years ago

Hi,

Firstly, can I please say what excellent work you've done with jqTree so far. I've gone through several different tree control projects in the last few days and this one is great and my favourite to work with.

I think I've come across an issue with the tree.init event - it doesn't seem to fire unless the tree is loaded via AJAX (using dataUrl). Well, more accurately, it fires, but the .bind()/.on() won't trigger because it doesn't seem to be attached to the tree element at the time?

A demonstration jsfiddle is here: http://jsfiddle.net/vf1yaL5d/ - If that is run with dataUrl instead of data, all of those events trigger, but when run with data the only event that triggers is the click (at least for me).

What I'm trying to do is to initialize the tree with either dummy data or a static list of top level nodes, and then populate it on demand via tree.select. I have tried setting dataUrl in the options and then manually calling loadDataFromUrl in the tree.select event, but that seems to perform two AJAX requests (one from the dataUrl parameter, and the second is the one I'm calling manually) - I think I can suppress that behavior one with event.preventDefault() but I don't know what else that will stop, so I'd like to avoid that if possible.

I hope that made sense - if anything requires clarification please let me know. I've just discovered #37 which might solve my issue, so I'll check that out in the mean time.

Cheers

m3rls commented 10 years ago

Just adding a note to say that using a function for dataUrl as described in #37 worked perfectly - which is awesome! I am still interested in why tree.init is not hitting the bind though.

mbraak commented 10 years ago

Hello,

The tree.init and tree.load_data events are not fired because the data is loaded before you bind the events.

You can fix this by binding the events first:

$('#tree1').on('tree.load_data', function(){ 
  alert('#tree1 tree.load_data fired'); 
}).tree({
  data: data
});

Also see the updated fiddle.

I can see that this is not intuitive. I don't know how to solve this though.

Greetings

m3rls commented 10 years ago

Hi!

Oh of course, that makes perfect sense! I think having the event binds above the .tree({}) initialization actually does make sense, I'm not sure why I didn't try reordering things first.

Thanks for your super quick reply, and again awesome work on this project!