From 4746a1af6349a34141289be17b952b1a17e5da3a Mon Sep 17 00:00:00 2001 From: Matt Date: Sun, 3 Aug 2014 10:37:20 +0100 Subject: [PATCH 1/3] Removing incorrect example of $.when() and AJAX. Adding documentation on the arguments passed to a master-Deferred. ... in both the fail and done cases. --- entries/jQuery.when.xml | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/entries/jQuery.when.xml b/entries/jQuery.when.xml index 3a147ab2..396b4c89 100644 --- a/entries/jQuery.when.xml +++ b/entries/jQuery.when.xml @@ -21,8 +21,36 @@ $.when( { testing: 123 } ).done(function( x ) { alert( x.testing ); // Alerts "123" }); -

In the case where multiple Deferred objects are passed to jQuery.when, the method returns the Promise from a new "master" Deferred object that tracks the aggregate state of all the Deferreds it has been passed. The method will resolve its master Deferred as soon as all the Deferreds resolve, or reject the master Deferred as soon as one of the Deferreds is rejected. If the master Deferred is resolved, it is passed the resolved values of all the Deferreds that were passed to jQuery.when. For example, when the Deferreds are jQuery.ajax() requests, the arguments will be the jqXHR objects for the requests, in the order they were given in the argument list.

-

In the multiple-Deferreds case where one of the Deferreds is rejected, jQuery.when immediately fires the failCallbacks for its master Deferred. Note that some of the Deferreds may still be unresolved at that point. If you need to perform additional processing for this case, such as canceling any unfinished ajax requests, you can keep references to the underlying jqXHR objects in a closure and inspect/cancel them in the failCallback.

+

In the case where multiple Deferred objects are passed to jQuery.when, the method returns the Promise from a new "master" Deferred object that tracks the aggregate state of all the Deferreds it has been passed. The method will resolve its master Deferred as soon as all the Deferreds resolve, or reject the master Deferred as soon as one of the Deferreds is rejected. If the master Deferred is resolved, the doneCallbacks for the master Deferred are executed. The arguments passed to the doneCallbacks provide the resolved values for each of the Deferreds, and matches the order the Deferreds were passed to jQuery.when. For example:

+

+var d1 = new $.Deferred();
+var d2 = new $.Deferred();
+
+$.when( d1, d2 ).done(function ( v1, v2 ) {
+    console.log( v1 ); // "Fish"
+    console.log( v2 ); // "Pizza"
+});
+
+d1.resolve( "Fish" );
+d2.resolve( "Pizza" );
+    
+

In the event a Deferred was resolved with no value, the corresponding doneCallback argument will be undefined. If a Deferred resolved to a single value, the corresponding argument will hold that value. If the case where a Deferred resolved to multiple values, the corresponding argument will be an array of those values. For example:

+

+var d1 = new $.Deferred();
+var d2 = new $.Deferred();
+var d3 = new $.Deferred();
+
+$.when( d1, d2, d3 ).done(function ( v1, v2, v3 ) {
+    console.log( v1 ); // v1 is undefined
+    console.log( v2 ); // v2 is "abc"
+    console.log( v3 ); // v3 is an array [ 1, 2, 3, 4, 5 ]
+});
+
+d1.resolve();
+d2.resolve( "abc" );
+d3.resolve( 1, 2, 3, 4, 5 );
+    
+

In the multiple-Deferreds case where one of the Deferreds is rejected, jQuery.when immediately fires the failCallbacks for its master Deferred. Note that some of the Deferreds may still be unresolved at that point. The arguments passed to the failCallbacks match the signature of the failCallback for the Deferred that was rejected. If you need to perform additional processing for this case, such as canceling any unfinished ajax requests, you can keep references to the underlying jqXHR objects in a closure and inspect/cancel them in the failCallback.

Execute a function after two ajax requests are successful. (See the jQuery.ajax() documentation for a complete description of success and error cases for an ajax request). From 9243feea70fc8ff64f808107d4104e24b07d93c8 Mon Sep 17 00:00:00 2001 From: Matt Date: Sun, 3 Aug 2014 10:38:38 +0100 Subject: [PATCH 2/3] Standardising inline function usage (jQuery.when -> jQuery.when()) --- entries/jQuery.when.xml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/entries/jQuery.when.xml b/entries/jQuery.when.xml index 396b4c89..f90dce9d 100644 --- a/entries/jQuery.when.xml +++ b/entries/jQuery.when.xml @@ -9,19 +9,19 @@ Provides a way to execute callback functions based on one or more objects, usually Deferred objects that represent asynchronous events. -

If a single Deferred is passed to jQuery.when, its Promise object (a subset of the Deferred methods) is returned by the method. Additional methods of the Promise object can be called to attach callbacks, such as deferred.then. When the Deferred is resolved or rejected, usually by the code that created the Deferred originally, the appropriate callbacks will be called. For example, the jqXHR object returned by jQuery.ajax() is a Promise and can be used this way:

+

If a single Deferred is passed to jQuery.when(), its Promise object (a subset of the Deferred methods) is returned by the method. Additional methods of the Promise object can be called to attach callbacks, such as deferred.then. When the Deferred is resolved or rejected, usually by the code that created the Deferred originally, the appropriate callbacks will be called. For example, the jqXHR object returned by jQuery.ajax() is a Promise and can be used this way:


 $.when( $.ajax( "test.aspx" ) ).then(function( data, textStatus, jqXHR ) {
   alert( jqXHR.status ); // Alerts 200
 });
     
-

If a single argument is passed to jQuery.when and it is not a Deferred or a Promise, it will be treated as a resolved Deferred and any doneCallbacks attached will be executed immediately. The doneCallbacks are passed the original argument. In this case any failCallbacks you might set are never called since the Deferred is never rejected. For example:

+

If a single argument is passed to jQuery.when() and it is not a Deferred or a Promise, it will be treated as a resolved Deferred and any doneCallbacks attached will be executed immediately. The doneCallbacks are passed the original argument. In this case any failCallbacks you might set are never called since the Deferred is never rejected. For example:


 $.when( { testing: 123 } ).done(function( x ) {
   alert( x.testing ); // Alerts "123"
 });
     
-

In the case where multiple Deferred objects are passed to jQuery.when, the method returns the Promise from a new "master" Deferred object that tracks the aggregate state of all the Deferreds it has been passed. The method will resolve its master Deferred as soon as all the Deferreds resolve, or reject the master Deferred as soon as one of the Deferreds is rejected. If the master Deferred is resolved, the doneCallbacks for the master Deferred are executed. The arguments passed to the doneCallbacks provide the resolved values for each of the Deferreds, and matches the order the Deferreds were passed to jQuery.when. For example:

+

In the case where multiple Deferred objects are passed to jQuery.when(), the method returns the Promise from a new "master" Deferred object that tracks the aggregate state of all the Deferreds it has been passed. The method will resolve its master Deferred as soon as all the Deferreds resolve, or reject the master Deferred as soon as one of the Deferreds is rejected. If the master Deferred is resolved, the doneCallbacks for the master Deferred are executed. The arguments passed to the doneCallbacks provide the resolved values for each of the Deferreds, and matches the order the Deferreds were passed to jQuery.when(). For example:


 var d1 = new $.Deferred();
 var d2 = new $.Deferred();
@@ -50,7 +50,7 @@ d1.resolve();
 d2.resolve( "abc" );
 d3.resolve( 1, 2, 3, 4, 5 );
     
-

In the multiple-Deferreds case where one of the Deferreds is rejected, jQuery.when immediately fires the failCallbacks for its master Deferred. Note that some of the Deferreds may still be unresolved at that point. The arguments passed to the failCallbacks match the signature of the failCallback for the Deferred that was rejected. If you need to perform additional processing for this case, such as canceling any unfinished ajax requests, you can keep references to the underlying jqXHR objects in a closure and inspect/cancel them in the failCallback.

+

In the multiple-Deferreds case where one of the Deferreds is rejected, jQuery.when() immediately fires the failCallbacks for its master Deferred. Note that some of the Deferreds may still be unresolved at that point. The arguments passed to the failCallbacks match the signature of the failCallback for the Deferred that was rejected. If you need to perform additional processing for this case, such as canceling any unfinished ajax requests, you can keep references to the underlying jqXHR objects in a closure and inspect/cancel them in the failCallback.

Execute a function after two ajax requests are successful. (See the jQuery.ajax() documentation for a complete description of success and error cases for an ajax request). From d1e3189fb1b26a0cd74b684db357650dd96a0a02 Mon Sep 17 00:00:00 2001 From: Matt Date: Sun, 3 Aug 2014 11:03:32 +0100 Subject: [PATCH 3/3] If the case -> In the case --- entries/jQuery.when.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entries/jQuery.when.xml b/entries/jQuery.when.xml index f90dce9d..e28819e3 100644 --- a/entries/jQuery.when.xml +++ b/entries/jQuery.when.xml @@ -34,7 +34,7 @@ $.when( d1, d2 ).done(function ( v1, v2 ) { d1.resolve( "Fish" ); d2.resolve( "Pizza" ); -

In the event a Deferred was resolved with no value, the corresponding doneCallback argument will be undefined. If a Deferred resolved to a single value, the corresponding argument will hold that value. If the case where a Deferred resolved to multiple values, the corresponding argument will be an array of those values. For example:

+

In the event a Deferred was resolved with no value, the corresponding doneCallback argument will be undefined. If a Deferred resolved to a single value, the corresponding argument will hold that value. In the case where a Deferred resolved to multiple values, the corresponding argument will be an array of those values. For example:


 var d1 = new $.Deferred();
 var d2 = new $.Deferred();