Can you stop a defered callback in jquery 1.5?

Posted by chobo2 on Stack Overflow See other posts from Stack Overflow or by chobo2
Published on 2011-03-02T23:08:48Z Indexed on 2011/03/02 23:24 UTC
Read the original article Hit count: 203

Filed under:
|

Hi

I am wondering say you have something like this

// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.ajax({ url: "example.php" })
    .success(function(response) { alert("success"); })


// perform other work here ...

// Set another success function for the request above
jqxhr.success(function(response){ alert("second success"); });

So I am thinking this. I have a general function that I want to use on all my responses that would be passed into my success.

This function basically does a check to see if the server validation found any errors. If it did they it formats it and displays a message.

Now I am wondering if I could some how have the second success function to then do specific stuff. Like say One ajax request needs to add a row into a table. So this should be possible. I just do what I have above and in the second success I just add the row.

Is it possible though that if the first success runs through and see that there are validation errors from the server that I can stop the second success from happening?

Sort of

If(first success finds errors)
{
   // print out errors
   // don't continue onto next success
}
else
{
   // go to next success 
}

Edit

I found that there is something call deferred.reject and this does stop it but I am wondering how can I specify to stop only the success one. Since my thinking is if there are other deffered ones like complete on it will the be rejected too?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about jquery1.5