Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

159 linhas
4.4KB

  1. define( [
  2. "./core",
  3. "./var/slice",
  4. "./callbacks"
  5. ], function( jQuery, slice ) {
  6. jQuery.extend( {
  7. Deferred: function( func ) {
  8. var tuples = [
  9. // action, add listener, listener list, final state
  10. [ "resolve", "done", jQuery.Callbacks( "once memory" ), "resolved" ],
  11. [ "reject", "fail", jQuery.Callbacks( "once memory" ), "rejected" ],
  12. [ "notify", "progress", jQuery.Callbacks( "memory" ) ]
  13. ],
  14. state = "pending",
  15. promise = {
  16. state: function() {
  17. return state;
  18. },
  19. always: function() {
  20. deferred.done( arguments ).fail( arguments );
  21. return this;
  22. },
  23. then: function( /* fnDone, fnFail, fnProgress */ ) {
  24. var fns = arguments;
  25. return jQuery.Deferred( function( newDefer ) {
  26. jQuery.each( tuples, function( i, tuple ) {
  27. var fn = jQuery.isFunction( fns[ i ] ) && fns[ i ];
  28. // deferred[ done | fail | progress ] for forwarding actions to newDefer
  29. deferred[ tuple[ 1 ] ]( function() {
  30. var returned = fn && fn.apply( this, arguments );
  31. if ( returned && jQuery.isFunction( returned.promise ) ) {
  32. returned.promise()
  33. .progress( newDefer.notify )
  34. .done( newDefer.resolve )
  35. .fail( newDefer.reject );
  36. } else {
  37. newDefer[ tuple[ 0 ] + "With" ](
  38. this === promise ? newDefer.promise() : this,
  39. fn ? [ returned ] : arguments
  40. );
  41. }
  42. } );
  43. } );
  44. fns = null;
  45. } ).promise();
  46. },
  47. // Get a promise for this deferred
  48. // If obj is provided, the promise aspect is added to the object
  49. promise: function( obj ) {
  50. return obj != null ? jQuery.extend( obj, promise ) : promise;
  51. }
  52. },
  53. deferred = {};
  54. // Keep pipe for back-compat
  55. promise.pipe = promise.then;
  56. // Add list-specific methods
  57. jQuery.each( tuples, function( i, tuple ) {
  58. var list = tuple[ 2 ],
  59. stateString = tuple[ 3 ];
  60. // promise[ done | fail | progress ] = list.add
  61. promise[ tuple[ 1 ] ] = list.add;
  62. // Handle state
  63. if ( stateString ) {
  64. list.add( function() {
  65. // state = [ resolved | rejected ]
  66. state = stateString;
  67. // [ reject_list | resolve_list ].disable; progress_list.lock
  68. }, tuples[ i ^ 1 ][ 2 ].disable, tuples[ 2 ][ 2 ].lock );
  69. }
  70. // deferred[ resolve | reject | notify ]
  71. deferred[ tuple[ 0 ] ] = function() {
  72. deferred[ tuple[ 0 ] + "With" ]( this === deferred ? promise : this, arguments );
  73. return this;
  74. };
  75. deferred[ tuple[ 0 ] + "With" ] = list.fireWith;
  76. } );
  77. // Make the deferred a promise
  78. promise.promise( deferred );
  79. // Call given func if any
  80. if ( func ) {
  81. func.call( deferred, deferred );
  82. }
  83. // All done!
  84. return deferred;
  85. },
  86. // Deferred helper
  87. when: function( subordinate /* , ..., subordinateN */ ) {
  88. var i = 0,
  89. resolveValues = slice.call( arguments ),
  90. length = resolveValues.length,
  91. // the count of uncompleted subordinates
  92. remaining = length !== 1 ||
  93. ( subordinate && jQuery.isFunction( subordinate.promise ) ) ? length : 0,
  94. // the master Deferred.
  95. // If resolveValues consist of only a single Deferred, just use that.
  96. deferred = remaining === 1 ? subordinate : jQuery.Deferred(),
  97. // Update function for both resolve and progress values
  98. updateFunc = function( i, contexts, values ) {
  99. return function( value ) {
  100. contexts[ i ] = this;
  101. values[ i ] = arguments.length > 1 ? slice.call( arguments ) : value;
  102. if ( values === progressValues ) {
  103. deferred.notifyWith( contexts, values );
  104. } else if ( !( --remaining ) ) {
  105. deferred.resolveWith( contexts, values );
  106. }
  107. };
  108. },
  109. progressValues, progressContexts, resolveContexts;
  110. // Add listeners to Deferred subordinates; treat others as resolved
  111. if ( length > 1 ) {
  112. progressValues = new Array( length );
  113. progressContexts = new Array( length );
  114. resolveContexts = new Array( length );
  115. for ( ; i < length; i++ ) {
  116. if ( resolveValues[ i ] && jQuery.isFunction( resolveValues[ i ].promise ) ) {
  117. resolveValues[ i ].promise()
  118. .progress( updateFunc( i, progressContexts, progressValues ) )
  119. .done( updateFunc( i, resolveContexts, resolveValues ) )
  120. .fail( deferred.reject );
  121. } else {
  122. --remaining;
  123. }
  124. }
  125. }
  126. // If we're not waiting on anything, resolve the master
  127. if ( !remaining ) {
  128. deferred.resolveWith( resolveContexts, resolveValues );
  129. }
  130. return deferred.promise();
  131. }
  132. } );
  133. return jQuery;
  134. } );