JQuery. Callbacks for jQuery1.8.2 source code parsing

Source: Internet
Author: User

JQuery. Callbacks for jQuery1.8.2 source code parsing has added detailed comments to the Code. If you need them, you can check the code below. In this way, it is much easier to use.

Code:

Copy to ClipboardReference: [www.bkjia.com] // String to Object options format cache
// It is a cache for option to avoid createOptions every time
// Every option is similar to this
//{
// Memory: true
//, Once: true
//,...
//}
Var optionsCache = {};

// Convert String-formatted options into Object-formatted ones and store in cache
Function createOptions (options ){
Var object = optionsCache [options] = {};
JQuery. each (options. split (core_rspace), function (_, flag ){
Object [flag] = true;
});
Return object;
}

/*
* Create a callback list using the following parameters:
*
* Options: an optional list of space-separated options that will change how
* The callback list behaves or a more traditional option object
*
* By default a callback list will act like an event callback list and can be
* "Fired" multiple times.
*
* Possible options:
*
* Once: will ensure the callback list can only be fired once (like a Deferred)
*
* Memory: will keep track of previous values and will call any callback added
* After the list has been fired right away with the latest "memorized"
* Values (like a Deferred)
*
* Unique: will ensure a callback can only be added once (no duplicate in the list)
*
* StopOnFalse: interrupt callings when a callback returns false
*
*/
JQuery. Callbacks = function (options ){

// Convert options from String-formatted to Object-formatted if needed
// (We check in cache first)
// Options can also be an object
Options = typeof options = "string "?
(OptionsCache [options] | createOptions (options )):
JQuery. extend ({}, options );

Var // Last fire value (for non-forgettable lists)
Memory,
// Flag to know if list was already fired
Fired,
// Flag to know if list is currently firing
Firing,
// First callback to fire (used internally by add and fireWith)
FiringStart,
// End of the loop when firing
FiringLength,
// Index of currently firing callback (modified by remove if needed)
FiringIndex,
// Actual callback list
List = [],
// Stack of fire CILS for repeatable lists
// Stack exists only when once is not set
// Stack is used to store parameter information (at this time, the function list is already in the firing state, you must store the parameters when you call fire elsewhere, and then run the fire command
Stack =! Options. once & [],
// Fire callbacks
Fire = function (data ){
// If memory is set, the data of each fire will be saved in memory and used as the parameter for the next call to add
Memory = options. memory & data;
Fired = true;
FiringIndex = firingStart | 0;
// Reset fireStarting to 0 because the add operation (memory) may change it
FiringStart = 0;
FiringLength = list. length;
Firing = true;
For (; list & firingIndex <firingLength; firingIndex ++ ){
// If the stopOnFalse parameter is set, when a function in the function list returns false, the execution of the subsequent function is stopped and memory is canceled (if set)
If (list [firingIndex]. apply (data [0], data [1]) ===false & options. stopOnFalse ){
Memory = false; // To prevent further callusing add
Break;
}
}
Firing = false;
If (list ){
// If the stack exists, extract the first parameter stored previously and continue the fire until the stack is empty.
If (stack ){
If (stack. length ){
Fire (stack. shift ());
}
// If the stack does not exist (that is, once is set)
// If memory is set, the previous function list will be cleared, that is, the memory is still in use. The add operation can penalize immediate execution of the function.
} Else if (memory ){
List = [];
} Else {
Self. disable ();
}
}
},
// Actual Callbacks object
// Actually returned Callbacks object
Self = {
// Add a callback or a collection of callbacks to the list
// Add a function or function set (array or pseudo array) to the function list
Add: function (){
If (list ){
// First, we save the current length
Var start = list. length;
(Function add (args ){
JQuery. each (args, function (_, arg ){
Var type = jQuery. type (arg );
// If arg is a function
// If unique is set, check whether the function already exists in the list. If unique exists, ignore it. Otherwise, push it to the list.
// If unique is not set, push it directly to the list
If (type = "function "&&(! Options. unique |! Self. has (arg ))){
List. push (arg );

// If arg is an array or a pseudo array, It is pushed to the list recursively.
} Else if (arg & arg. length & type! = "String "){
// Inspect recursively
// Recursion (member: function set)
Add (arg );
}
});
}) (Arguments );
// Do we need to add the callbacks to
// Current firing batch?
// When adding a function (SET)

// If the function list is in the firing status, execute the add (fn) operation in a callback.
// Modify fireLength immediately so that the function list can be executed to the newly added function (SET) in the fire)
If (firing ){
FiringLength = list. length;
// With memory, if we're not firing then
// We shocould call right away
// If it is not the firing status and memory is set (this step must be executed only when the fired status is enabled, because memory will be negative only once after the fire status)
// At this time, memory is already the parameter passed by the last fire, so the newly added function set will be executed directly without the fire
} Else if (memory ){
FiringStart = start;
Fire (memory );
}
}
Return this;
},
// Remove a callback from the list
// Delete a function (SET) from the function list)
Remove: function (){
If (list ){
JQuery. each (arguments, function (_, arg ){
Var index;
// The while LOOP indicates that the same function reference in the function list is deleted by using the powerful jQuery. inArray (unique is not set)
// JQuery. inArray uses the index of the searched element every time it is returned as its third parameter to continue searching until the end of the function list.
// Splice deletes array elements and modifies the structure of the array.
While (index = jQuery. inArray (arg, list, index)>-1 ){
List. splice (index, 1 );
// Handle firing indexes
// When the function list is in the firing state, the most important thing is to maintain the values firingLength and firgingIndex.
// Ensure that the functions in the function list in the fire operation can be correctly executed (The for loop in the fire operation requires these two values)
If (firing ){
If (index <= firingLength ){
FiringLength --;
}
If (index <= firingIndex ){
FiringIndex --;
}
}
}
});
}
Return this;
},
// Control if a given callback is in the list
// Determine whether the function list can only contain the given fn
Has: function (fn ){
Return jQuery. inArray (fn, list)>-1;
},
// Remove all callbacks from the list
// Clear the function list
Empty: function (){
List = [];
Return this;
},
// Have the list do nothing anymore
// Invalidate the function list (nothing can be done again)
Disable: function (){
List = stack = memory = undefined;
Return this;
},
// Is it disabled?
// Determine whether the function list is disabled
Disabled: function (){
Return! List;
},
// Lock the list in its current state
Lock: function (){
Stack = undefined;
If (! Memory ){
Self. disable ();
}
Return this;
},
// Is it locked?
Locked: function (){
Return! Stack;
},
// Call all callbacks with the given context and arguments
// Similar to fire, fireWith can give context parameters.
// In the fire, the context in the call to fireWith is this (function list object self)
FireWith: function (context, args ){
Args = args | [];
Args = [context, args. slice? Args. slice (): args];
// Fire at least once
// If you run fired once, store the Context Environment and parameters if the stack exists (that is, once is not set); otherwise, do nothing
If (list &&(! Fired | stack )){
If (firing ){
Stack. push (args );
} Else {
Fire (args );
}
}
Return this;
},
// Call all the callbacks with the given arguments
// Execute the functions in the function list in sequence
Fire: function (){
Self. fireWith (this, arguments );
Return this;
},
// To know if the callbacks have already been called at least once
// Determine whether the function list has been fired (even once)
Fired: function (){
Return !! Fired;
}
};

Return self;
};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.