Here is my code :
function expandResultNodes(responseText) {
var result_keys=[];
$.each(responseText, function( index, keypath) {
result_keys.push(keypath.split("/").pop());
});
var FTPtree = $("#Tree_sasFTP").fancytree("getTree") ;
// Use deferred promise:
FTPtree.loadKeyPath(responseText).done(function(){
defferedFilterNodes(FTPtree);
});
// FTPtree.loadKeyPath(responseText).done(
// defferedFilterNodes(FTPtree)
// );
function defferedFilterNodes (tree){
tree.filterNodes(function(node) {
console.log("filtering " + node.key);
return result_keys.some(function(element) {
// checks whether a result_keys element equals node key
return element == node.key;
});
});
}
}
I send an AJAX request to my server, search is made on a part of its file system, and server response is a JSON array made of key paths. Example :
0 "/_f83b45b7c4d40be3b25b7abfcb7226c4/_eb8cf44ea2366ae3cf1af572d5a818e0/_393d735d160b9d21ddde00b739991e59"
1 "/_f83b45b7c4d40be3b25b7abfcb7226c4/_eb8cf44ea2366ae3cf1af572d5a818e0/_32a9a6fc7652124f1882f52e2316ee0e"
2 "/_f83b45b7c4d40be3b25b7abfcb7226c4/_eb8cf44ea2366ae3cf1af572d5a818e0/_32a9a6fc7652124f1882f52e2316ee0e/_19a2fd29b73244e41f9eaa4ec6dc5e79/_fb58a0b2d0e222117b83e361be8643e4"
3 "/_f83b45b7c4d40be3b25b7abfcb7226c4/_eb8cf44ea2366ae3cf1af572d5a818e0/_32a9a6fc7652124f1882f52e2316ee0e/_19a2fd29b73244e41f9eaa4ec6dc5e79/_32a1178799b476d197251c2d1e617903"
[...]
24 "/_f83b45b7c4d40be3b25b7abfcb7226c4/_eb8cf44ea2366ae3cf1af572d5a818e0/_f45e1e6c72e2e8a80889794671538c6e/_11fe4e4e36adeacf04d8efbffcf4259a/_16b6520368113327455a2574562a902f"
25 "/_f83b45b7c4d40be3b25b7abfcb7226c4/_eb8cf44ea2366ae3cf1af572d5a818e0/_123fcfdc7ff35b145093d9ff49ada47b"
function FTPtree.loadKeyPath(responseText) will lazy load any non-existing node from the server response.
And then I filter them to show result of the search.
Results of the search are only the last keys of the key paths array. So this array gives two things : paths to the results and results keys (result_keys array) .
Once every path is loaded, I filter nodes using defferedFilterNodes function that is a custom matcher callback.
The thing that I don't understand -- and I don't know if it's due to jquery promises mechanisms, fundamentals of JavaScript or the way fancytree is coded -- the thing I don't understand is that when I call defferedFilterNodes itself, it seems that it only sees Tree as the state it was before loadkeypath, and when I call defferedFilterNodes inside an IIFE, it sees Tree as it really is at the end of loadkeypath execution.
May someone explain me that ?
Thanks in advance
Best regards
Fred
FTPtree.loadKeyPath(responseText).done(function(){
defferedFilterNodes(FTPtree);
});
FTPtree.loadKeyPath(responseText).done(function(result){
defferedFilterNodes(FTPtree);
});
FTPtree.loadKeyPath(responseText).done(
defferedFilterNodes(FTPtree);
);
Late answer sorry ...
Thank you so much for your answer.
It brought me to view and review again many basic concepts of JavaScript, and I feel now much more comfortable with this language. Not a rapid review, but a real work on the very basic concepts.
I discovered how I was able to get results from JavaScript and javascript libraries and so on, for years, but with a lot of misunderstanding on the language fundamentals.
This little - and probably trivial for many - question led me far in the Javascript language and this trip will not be soon completed.
Anyway thousand thanks mar10 for your so good work on Fancytree, it's really a great, amazing, powerful tool.
Best regards
Fred