Search code examples
jqueryjquery-1.5

Why have multiple callback(success,error) in jquery 1.5


I saw today that jquery 1.5.1 has been released!!

It seems 2 changes have been made(ajax, support).

I been looking at the new ajax features and I am kinda confused why you would want to setup your callback at a later point?

Also I don't get why you would want multiple callbacks. Why would you want 5 success call backs? Can you set it up to use certain call backs on different data coming back from the data or does it just do all 5 of them?


Solution

  • http://www.erichynds.com/jquery/using-deferreds-in-jquery/ has a great overview, I recommend you give it a read and check out the examples. However, it also ends with an excellent summary:

    Deferreds introduce a new, robust approach to writing asynchronous tasks. Instead of focusing on how to organize callback logic into a singular callback, you can assign several individual actions to callback queues knowing that these will be executed, in context, without worrying so much about synchronicity.

    Another good page on jQuery 1.5 explains:

    The ajax module has been rewritten entirely. It will now return a special jQuery based XHR object (rather than a plain XHR object). This object is a deferred object that I spoke about above. This means we can pass the ajax request around and let components or modules attach their own handlers when they need to. We can also bind multiple callbacks via the jQuery chaining pattern.

    (Emphasis mine.)