jquery.deferred, fancytree, and IIFE

39 views
Skip to first unread message

fdum...@gmail.com

unread,
Mar 21, 2018, 1:42:59 PM3/21/18
to Fancytree Q&A
Hi
A question about something that works good but that I'd like to understand.
I'm not sure it is related to Fancytree, however I see this phenomena with Fancytree.

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

mar10

unread,
Mar 22, 2018, 3:37:40 AM3/22/18
to Fancytree Q&A
done() requires a function as argument, that gets called when the deferred resolves.
Here you are passing an anonymous function (not an IIFE), which is correct:

FTPtree.loadKeyPath(responseText).done(function(){
    defferedFilterNodes
(FTPtree);
});

That callback also is called with some result data, so this is the complete form:
FTPtree.loadKeyPath(responseText).done(function(result){
    defferedFilterNodes
(FTPtree);
});

Here you are passing the result of a function call (whatever defferedFilterNodes() returns). This will not work:

FTPtree.loadKeyPath(responseText).done(
    defferedFilterNodes
(FTPtree);
);



fdum...@gmail.com

unread,
Apr 12, 2018, 10:53:41 AM4/12/18
to Fancytree Q&A
Hi mar10

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

mar10

unread,
Apr 12, 2018, 3:17:16 PM4/12/18
to Fancytree Q&A
I was at the same point some time ago - as most JS programmers, I guess.
It takes a while until the (;{[]}) mess starts to make sense, but once you grok it, JavaScript unfolds its special charm ;-)
Thanks for the kind feedback.
Reply all
Reply to author
Forward
0 new messages