0

I have two functions, one is for expanding tree view (i.e. ExpandAll()) and second one is for removing particular type of elements from that tree view (i.e. RemoveAbElements()).

ExpandAll() method checks if there are child nodes under the selected node. If not then it retrieves the child elements by ajax call. So now I am calling these methods as follows :

function(){
    ExpandAll();

    RemoveAbElements();
}

Now my problem here is, there is a callback in ExpandAll() method and it gets called for each child node expanded (which is expected). Now here the callback gets called even after the execution of RemoveAbElements() method. I want to execute ExpandAll() method and all of its callbacks before RemoveAbElements() execution. I tried lots of things for this but none worked. please help.

1
  • Sounds like you've overcomplicated your design to me. To me it sounds like your problem is at least one of your design decisions: lazy-loading your tree view (don't do this unless your data sets are enormous), asynchronous lazy-loading (if you want it to be asynchronous, then you need to put your post-processing into your onreadystatechange event handler), or requesting too much data which requires data to be filtered out from the response once you've received it (get the server to filter the data out if it's not going to be required). Commented May 23, 2013 at 8:41

2 Answers 2

1

There could a be lot of ways you could be approaching.

One way could be, passing the RemoveAbElements itself to ExpandAll.

So you could be passing it as

ExpandAll(RemoveAbElements);

or, When you don't want to call RemoveElements, as :

ExpandAll(); 

And ExpandAll could be modified to accept the callback :

ExpandAll(callbackFunc) {

   //... Do Work Here

   if(callbackFunc) callbackFunc();
}

Or use triggerhandler & on if there is a jquery object, as suggested by slinky2000.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks loxxy for your reply, but ExpandAll() callback will be called multiple times. and I wanted to call RemoveAbElements() only once. Will it work like this?
you can leave that argument null, for cases when you dont want to call RemoveElements. I've updated my example...
0

If I've understood you correctly you need to add a listener to EXpandAll() and when it's finished everything call RemoveAbElements()

I would look at jquerys custom event triggering:

http://api.jquery.com/trigger/

// Listen for finish
$('#tree').on('customFinishedEvent', function(event) {
    alert('finished');
});

// On finished loading and expanding
$("#tree").trigger('customFinishedEvent');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.