tags:

views:

222

answers:

1

i have this element to which various change events are applied how can i trigger my own function after all change events have occurred in mootools

a sample scenario is a drop down list

$('ddl').addEvent('change', function () { // some ajax call going on here and which i can't affect // as the page is generated that way });

i now want to call my function right after the change event is done but adding another change event will not do as i believe the events are executed async

A: 

pass on a callback function through the onComplete of the ajax that you reference.

eg:

$("ddl").addEvents({
    change: function() {
        new Request({
           method: "get",
           url: "somefile.php",
           onComplete: function() {
               // run your next stuff from within here, referencing 
               // this.response.text or whatevever and call up another function.
           }
        }).send("action=checkuser&id="+this.get("value").trim());
    }
});

there's also the Chain class - http://mootools.net/docs/core/Class/Class.Extras#Chain but as you have async stuff going on, you really need the onComplete on the Request.

Dimitar Christoff
@Dimitar, thanks for the tip but as I noted (maybe not clearly enough) in my question, I can't affect how the event is added to $('ddl') but still would need to do this after the change event already added
Gobezu Sewu
if that's the case, you are not in a good position. basically, as you rightly point out, adding a new change event is async from the existing one. your one option is, if the Request method is recycled, i.e. the instance is stored into a variable you can access from an external scope, you can add a second change event that `addEvent`'s `onComplete` on the Request instance to pass it back to you. otherwise, you need to do a loop that checks the outcome of the Request through ().periodical(nnn), say every 50ms until you are certain it's done before running your next block of code.
Dimitar Christoff
here's an example of the above: http://www.jsfiddle.net/9ZJst/if not, then here is how you can fix it by using periodical checks of an example content change of a layer (through ajax update) - http://www.jsfiddle.net/q6rcP/good luck
Dimitar Christoff
@Dimitar, this thing have taken a good deal of my day and have now resorted to your solution with periodical, by doing some simple checks before triggering my actual call, but its something one would expect to have in such framework, thank you!
Gobezu Sewu
it is unfair to say the framework's at fault here rather than your design requirements, if you are abstracting your code on top of something else you need to work around the limitations and no framework will help break into a 'foreign' scope / event queue to help you do that. if you really wanted, you could do `$("ddl").removeEvents()` and then add them again, refactored to work as per your own tastes, overriding the original js
Dimitar Christoff