JQuery journal 6.9 Promise/A Callbacks

Source: Internet
Author: User

JQuery does not simply use an Array to store callback functions, but uses JQuery. callbacks (options) returns a self object, which can dynamically add, remove, and fire callback function queues. this function must be noted as the options parameter. It is a string, which is composed of any combination of the four parameters. options: once: the callback function only executes memory once: parameters passed in when the callback function is used to call add. unique: the callback function can only be added to the queue once. stopOnFlase: One callback function returns false. Stop executing the following callback function.
JQuery. variables in Callbacks (options) have different meanings based on different passing parameters. These functions are referenced by closures to expand their lifecycles to change their values externally. memory is described below: if options does not contain the 'memory 'string, memeory is false, and other parameters are the last fire (args) parameter. fired: whether the callback function list has been triggered. firing: whether the callback function list is being triggered. why is this parameter required? Because if the add, remove, and fire methods are called in the callback function, the status of the callback function list will be changed (start trigger position, trigger length, and so on, these are the symbols that control the functions in the callback function list to be executed. At this time, the callback function list is being triggered and must be reflected in a timely manner (corrected the trigger position, trigger length, etc) changes in the callback function list, so that the next iteration In the callback function execution cycle can correctly trigger the callback function list. firingStart: the start position of the execution of the callback function list. firingLength: Execute several callback functions firingIndex: Position of the function being triggered in the callback function list: stores the callback function stack. If the fire or fireWith method is called in the callback function, import the context and parameters passed in by fire or fireWith into the stack, and then use the parameters in the stack as the parameters to execute the fire or fireWidth called in the callback function.
Note: fire or fireWidth is called in the callback function when we go to the stack. push branch, but this will lead to an endless loop. What is the problem with this stack? Please let me know !!

JQuery. callbacks = function (options) {// Convert options from String-formatted to Object-formatted if needed // (we check in cache first) 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, // Flag to know if list is currently firing, // First callback to fire (used internally by add and fireWith) firingStart, // End of the loop when firing firingLength, // Index of currently firing c Allback (modified by remove if needed) firingIndex, // Actual callback list = [], // Stack of fire CILS for repeatable lists // if once is not configured, stack is an array // when the stack is retained, the fire parameter stack =! Options. once & [], // Fire callbacks fire = function (data) {// The memory = options parameter when memory is false or fire. memory & data; fired = true; // The current trigger position firingIndex = firingStart | 0; // the start position of the trigger callback function queue, firingStart = 0; // The queue length of the callback function to be triggered: firingLength = list. length; // The ID being triggered is set to true // This value is true firing = true only in the Callbacks callback function; for (; list & firingIndex <firingLength; firingIndex ++) {// If stopOnFalse is configured, when the callback function team The returned value of the function in the column is false if (list [firingIndex]. apply (data [0], data [1]) ===false & options. stopOnFalse) {// memory is set To false memory = false; // To prevent further callusing add // The subsequent callback function break is no longer executed ;}} // The execution of the callback function ends with firing = false; if (list) {// once if (stack) Not Configured {// The fire if (stack) called in the callback function is executed. length) {fire (stack. shift ();} // clear the callback function list, but this callbacks is still available} else if (memory) {list = []; // Let the closure that Some references can be destroyed} else {self. disable () ;}}, // Actual Callbacks object self ={// Add a callback or a collection of callbacks to the list add: function () {if (list) {// First, we save the current length // before adding the callback function, set the length of the current callbacks to start var start = list. length; (function add (args) {jQuery. each (args, function (_, arg) {var type = jQuery. type (arg); if (type = "function") {// if uniq is not set Ue or the callback function list does not contain this function // Add this function in the callback function queue if (! Options. unique |! Self. has (arg) {list. push (arg) ;}// if arg is a class array, recursively add} else if (arg & arg. length & type! = "String") {// Inspect recursively add (arg) ;}) (arguments ); // Do we need to add the callbacks to the // current firing batch? // Calling the add method in the callback function being triggered will go through this branch if (firing) {// immediately reflects the change in the length of the callback function to be triggered firingLength = list. length; // With memory, if we're not firing then // we shocould call right away} else if (memory) {// change the trigger start position to firingStart = start; fire (memory) ;}return this ;}, // Remove a callback from the list remove: function () {if (list) {jQuery. each (arguments, function (_, arg) {// index of the list in which arg is located var ind Ex; while (index = jQuery. inArray (arg, list, index)>-1) {// Delete list. splice (index, 1); // Handle firing indexes // if you call remove if (firing) in the callback function) {// The callback function to be deleted is the if (index <= firingLength) that has not been triggered {// The length of the trigger minus firingLength --;} // you need to delete the trigger function if (index <= firingIndex) {// you want to trigger the index minus one firingIndex --;}}}});} return this;}, // Check if a given callback is in the list. // If no argument is Given, return whether or not list has callbacks attached. has: function (fn) {return fn? JQuery. inArray (fn, list)>-1 :!! (List & list. length) ;}, // Remove all callbacks from the list empty: function () {list = []; firingLength = 0; return this ;}, // Have the list do nothing anymore // the callback function list function is completely invalid disable: function () {list = stack = memory = undefined; return this ;}, // Is it disabled? Disabled: function () {return! List;}, // Lock the list in its current state // lock the current state and will not call fire or fireWidth Lock: function () {stack = undefined in the callback function; if (! Memory) {self. disable ();} return this ;}, // Is it locked? Locked: function () {return! Stack;}, // Call all callbacks with the given context and arguments fireWith: function (context, args) {// if (list &&(! Fired | stack) {args = args | []; args = [context, args. slice? Args. slice (): args]; //? In fact, this is a bit confusing. Go to this point // call fire or fireWidth In the callback function, but this will lead to an endless loop if (firing) {stack. push (args) ;}else {fire (args) ;}return this ;}, // Call all the callbacks with the given arguments fire: function () {self. fireWith (this, arguments); return this ;}, // To know if the callbacks have already been called at least 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.