Tuesday 20 December 2016

jquery - What are deferred objects?



jQuery 1.5 adds "Deferred Objects". What are they, and what exactly do they do?


Answer



Deferred Object



As of jQuery 1.5, the Deferred object provides a way to register multiple callbacks into self-managed callback queues, invoke callback queues as appropriate, and relay the success or failure state of any synchronous or asynchronous function.



Deferred Methods:






Deferred In Action:



$.get("test.php").done(
function(){ alert("$.get succeeded"); }
);

$.get("test.php")

.done(function(){ alert("$.get succeeded"); })
.fail(function(){ alert("$.get failed!"); });


And it seems that the existing ajax() method callbacks can be chained rather than declared in the settings:



var jqxhr = $.ajax({ url: "example.php" })
.success(function() { alert("success"); })
.error(function() { alert("error"); })
.complete(function() { alert("complete"); });






Working Example From Eric Hynds blog post: http://jsfiddle.net/ehynds/Mrqf8/






jqXHR




As of jQuery 1.5, the $.ajax() method returns the jXHR object, which is a superset of the XMLHTTPRequest object. For more information, see thejXHR section of the $.ajax entry







From JQUERY 1.5 RELEASED:



DEFERRED OBJECTS



Along with the rewrite of the Ajax

module a new feature was introduced
which was also made publicly
available: Deferred Objects. This
API allows you to work with return
values that may not be immediately
present (such as the return result
from an asynchronous Ajax request).
Additionally it gives you the ability
to attach multiple event handlers
(something that wasn’t previously

possible in the Ajax API).



Additionally you can make your own
deferred objects using the exposed
jQuery.Deferred. More information
about this API can be found in the
Deferred Object documentation.



Eric Hynds has written up a good
tutorial on Using Deferreds in jQuery

1.5
.



No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...