How to use deferred with custom event

70 views Asked by At

I am writing a plugin which emits the events activating and activated. I want to trigger the event activated only when all event handlers of activating are executed. I know that $.trigger() calls the event handlers synchronously, but the problem is I may have some async code in my event handler:

Example:

$("test").trigger("activating");
$("test").trigger("activated");


$("test").on("activating",function()
{
     $.post("/test",{data:"test"},function()
    {
            //dom modifications here
            ////////////////
            //Here I would like to notify that the event handler is finished, like maybe async.js
    }
});

I have a few clues about $.Deferred & Promises, but I don't see how to implement them to resolve my specific problem.

Could somebody here give me the solution?

Thanks

0

There are 0 answers