lazyload in fancytree, pre lazy load event

1.2k views Asked by At

I am using the fancytree plugin to render a directory structure and I am trying to get lazyLoad working.

lazyLoad: function (event, data) {
    data.result = {
        url: 'get_tree',
        data: {'key':data.node.key}
    };
}

Even though the name get_tree suggests that a tree at the given key is returned, it does not. Our website is set up in such a way that get_tree is dispatched as a task to a random server in the pool and instead a job id is returned. In order to get the actual tree, I have a function called poll_job that will continuously poll the job until it's done and the tree is returned.

Obviously the code above excepts the tree right away, but that's not the case here. How do I handle this? Is there some pre lazyLoad event I can attach to fancytree which will be first triggered before 'lazyLoad'. And here, I would kick off get_tree and pass that task id to lazyLoad. Is there another way to do this?

1

There are 1 answers

0
mar10 On

data.result accepts deferred promises, so you can return a promise and resolve it as soon as data finally arrives:

$("#tree").fancytree({
  source: ...,
  lazyLoad: function(event, data){
    // Immediately return a deferred and resolve it as soon as data is available:
    var dfd = new $.Deferred();
    data.result = dfd.promise();
    window.setTimeout(function(){  // Simulate a slow ajax request
      dfd.resolve([ // Resolve with final data
        { title: "node 1", lazy: true },
        { title: "node 2", select: true }
      ]);
    }, 1500);
  },
  [...]
});

See https://github.com/mar10/fancytree/wiki/TutorialLoadData for details.