首页 > 代码库 > jquery1.7.2的源码分析(四)$.Deferred(2)

jquery1.7.2的源码分析(四)$.Deferred(2)

jQuery.Callbacks = function( flags ) {// Convert flags from String-formatted to Object-formatted// (we check in cache first)flags = flags ? ( flagsCache[ flags ] || createFlags( flags ) ) : {};var // Actual callback listlist = [],// Stack of fire calls for repeatable listsstack = [],// Last fire value (for non-forgettable lists)memory,// Flag to know if list was already firedfired,// Flag to know if list is currently firingfiring,// First callback to fire (used internally by add and fireWith)firingStart,// End of the loop when firingfiringLength,// Index of currently firing callback (modified by remove if needed)firingIndex,// Add one or several callbacks to the listadd = function( args ) {var i,length,elem,type,actual;for ( i = 0, length = args.length; i < length; i++ ) {elem = args[ i ];type = jQuery.type( elem );if ( type === "array" ) {// Inspect recursivelyadd( elem );} else if ( type === "function" ) {// Add if not in unique mode and callback is not inif ( !flags.unique || !self.has( elem ) ) {list.push( elem );}}}},// Fire callbacksfire = function( context, args ) {args = args || [];memory = !flags.memory || [ context, args ];fired = true;firing = true;firingIndex = firingStart || 0;firingStart = 0;firingLength = list.length;for ( ; list && firingIndex < firingLength; firingIndex++ ) {if ( list[ firingIndex ].apply( context, args ) === false && flags.stopOnFalse ) {memory = true; // Mark as haltedbreak;}}firing = false;if ( list ) {if ( !flags.once ) {if ( stack && stack.length ) {memory = stack.shift();self.fireWith( memory[ 0 ], memory[ 1 ] );}} else if ( memory === true ) {self.disable();} else {list = [];}}},// Actual Callbacks objectself = {// Add a callback or a collection of callbacks to the listadd: function() {if ( list ) {var length = list.length;add( arguments );// Do we need to add the callbacks to the// current firing batch?if ( firing ) {firingLength = list.length;// With memory, if we‘re not firing then// we should call right away, unless previous// firing was halted (stopOnFalse)} else if ( memory && memory !== true ) {firingStart = length;fire( memory[ 0 ], memory[ 1 ] );}}return this;},// Remove a callback from the listremove: function() {if ( list ) {var args = arguments,argIndex = 0,argLength = args.length;for ( ; argIndex < argLength ; argIndex++ ) {for ( var i = 0; i < list.length; i++ ) {if ( args[ argIndex ] === list[ i ] ) {// Handle firingIndex and firingLengthif ( firing ) {if ( i <= firingLength ) {firingLength--;if ( i <= firingIndex ) {firingIndex--;}}}// Remove the elementlist.splice( i--, 1 );// If we have some unicity property then// we only need to do this onceif ( flags.unique ) {break;}}}}}return this;},// Control if a given callback is in the listhas: function( fn ) {//判断是否已经存在这个函数if ( list ) {var i = 0,length = list.length;for ( ; i < length; i++ ) {if ( fn === list[ i ] ) {return true;}}}return false;},// Remove all callbacks from the listempty: function() {//清空函数组list = [];return this;},// Have the list do nothing anymoredisable: function() {//使得不可能list = stack = memory = undefined;return this;},// Is it disabled?disabled: function() {//判断是不是不可能了return !list;},// Lock the list in its current statelock: function() {stack = undefined;if ( !memory || memory === true ) {self.disable();}return this;},// Is it locked?locked: function() {return !stack;},// Call all callbacks with the given context and argumentsfireWith: function( context, args ) {if ( stack ) {if ( firing ) {//不是只执行一次if ( !flags.once ) {stack.push( [ context, args ] );}} else if ( !( flags.once && memory ) ) {fire( context, args );}}return this;},// Call all the callbacks with the given argumentsfire: function() {self.fireWith( this, arguments );return this;},// To know if the callbacks have already been called at least oncefired: function() {//是否已经执行完了return !!fired;}};return self;};

jquery1.7.2的源码分析(四)$.Deferred(2)