четверг, 16 января 2014 г.

Learning jQuery Deferreds

Started to learn Learning jQuery Deferreds.

The aim of the book is to give the high level overview of the deferreds and their implementation in jQuery particularly.
Here are the benefits of this book:
1) Introduction to promises and deferreds (through nice informal analogies). No special skills are necessary.
2) A lot of practical ready recipes. You can apply them immediately.
3) Exercises and answers.
4) The size of the book is rather small. You can read it fast.

After reading this book you will have another tool in your belt. Deffereds  are not the silver bullet, but sometimes is will allow you to write maintainable javascript code.

Some examples on deferreds application can be found on http://learn.jquery.com/code-organization/deferreds/examples/.

Update 08.07.2014: jQuery contains useful method $.when. Provides a way to execute callback functions based on one or more objects, usually Deferred objects that represent asynchronous events. In practice you can pass it multiple deffereds and the method will resolve as soon as all the Deferreds resolve, or reject as soon as one of the Deferreds is rejected.

So, if you pass some ajax requests and want to wait when ALL of them will be finished with success or failed you can't use $.when.

I have find out the following solution to this in the net:

$(function(){
 $.whenAll = function( firstParam ) {
     var args = arguments,
         sliceDeferred = [].slice,
         i = 0,
         length = args.length,
         count = length,
         rejected,
         deferred = length <= 1 && firstParam && jQuery.isFunction( firstParam.promise )
             ? firstParam
             : jQuery.Deferred();

     function resolveFunc( i, reject ) {
         return function( value ) {
             rejected = true;
             args[ i ] = arguments.length > 1 ? sliceDeferred.call( arguments, 0 ) : value;
             if ( !( --count ) ) {
                 // Strange bug in FF4:
                 // Values changed onto the arguments object sometimes end up as undefined values
                 // outside the $.when method. Cloning the object into a fresh array solves the issue
                 var fn = rejected ? deferred.rejectWith : deferred.resolveWith;
                 fn.call(deferred, deferred, sliceDeferred.call( args, 0 ));
             }
         };
     }

     if ( length > 1 ) {
         for( ; i < length; i++ ) {
             if ( args[ i ] && jQuery.isFunction( args[ i ].promise ) ) {
                 args[ i ].promise().then( resolveFunc(i), resolveFunc(i, true) );
             } else {
                 --count;
             }
         }
         if ( !count ) {
             deferred.resolveWith( deferred, args );
         }
     } else if ( deferred !== firstParam ) {
         deferred.resolveWith( deferred, length ? [ firstParam ] : [] );
     }
     return deferred.promise();
 };
});

This function allows you to pass multiple deffereds and wait until all of them will be resolved or rejected.

Комментариев нет:

Отправить комментарий