Tuesday, 10 October 2017

jquery - What are deferred objects?

itemprop="text">

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


class="post-text" itemprop="text">
class="normal">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 href="http://www.erichynds.com/jquery/using-deferreds-in-jquery/"
rel="noreferrer">From Eric Hynds blog post: href="http://jsfiddle.net/ehynds/Mrqf8/"
rel="noreferrer">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 href="http://blog.jquery.com/2011/01/31/jquery-15-released/" rel="noreferrer">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: href="http://api.jquery.com/category/deferred-object/" rel="noreferrer">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
href="http://api.jquery.com/category/deferred-object/" rel="noreferrer">Deferred
Object documentation.



Eric Hynds has
written up a good
tutorial on href="http://www.erichynds.com/jquery/using-deferreds-in-jquery/"
rel="noreferrer">Using Deferreds in jQuery
1.5.



No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...