scratch – Blame information for rev 125

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