jquery Source---(2880, 3042) Callbacks: Callback object: Unified management of functions

Source: Internet
Author: User

//Optionscache: {' Once memory ': {once:true, memory:true}}varOptionscache = {};//once Memory,options.match (Core_rnotwhite) =[once, Memory],function (_, Flag) ={once:true,memory:true}functioncreateoptions (options) {varObject = optionscache[Options] = {};//The brackets are the JSON dots, and there is no point. Jquery.each (Options.match (core_rnotwhite) | | [],function(_, flag) {object[flag]=true;    }); returnobject;}/*var cb = $.    Callbacks ();    function aaa () {alert (1);    } cb.add (AAA);        (function () {function bbb () {alert (2);    } cb.add (BBB);    })(); Cb.fire ();//1 2*/Jquery.callbacks=function(options) {//static methods for classes    //options available: Once memory unique Stoponfalse    //method: Add remove has an empty disable disabled lock locked Firewith fire fired    //don't write anything. Jquery.extend ({}, Options), return empty {}, do not write this, if options===undefined, later also do compatibility. Options =typeofoptions = = = "string"?(optionscache[options]||createoptions (Options)): Jquery.extend ({}, options);//Options={once:true,memory:true}    varmemory, fired, firing, Firingstart, Firinglength, Firingindex, //all the methods addedList =[], Stack=!options.once &&[], fire=function(data) {memory= Options.memory && data;//have memory return trueFired =true;//Trigger StartFiringindex = Firingstart | | 0; Firingstart= 0; Firinglength=list.length; Firing=true;  for(; list && Firingindex < firinglength; firingindex++ ) {                if(list[firingindex].apply (data[0], data[1]) = = =false&& options.stoponfalse) {//function returns FALSE, Stoponfalse property does not execute downMemory =false;//memory will also be set to False                     Break; }} Firing=false;//Trigger End            if(list) {if(Stack) {if(stack.length) {Fire (Stack.shift ()); }                } Else if(memory) {list= []; } Else{self.disable (); }}}, self= {//for external interfacesAddfunction() {                if(list) {//The list comes up with an empty array, which returns True,                    varStart =list.length; (functionAdd (args) {//args is a formal parameterJquery.each (args,function(_, Arg) {//traversal of multiple method names passed in,                            varType =Jquery.type (ARG); if(Type = = = "function" ) {                                if(!options.unique | |!self.has (ARG)) {//not Unioqye, or unipue, but no.List.push (ARG); }                            } Else if(Arg && arg.length && type!== "string") {//arrays, nestingAdd (ARG);                    }                        }); }) (arguments);//arguments is an argument, method name AAA,                    if(firing) {Firinglength=list.length; } Else if(memory) {//The first time no assignment is undefined,Firingstart =start;                    Fire (memory); }                }                return  This; }, remove:function() {                if(list) {Jquery.each (arguments,function(_, Arg) {//You can delete multiple                        varindex;  while(index = Jquery.inarray (ARG, list, index)) >-1) {list.splice (index,1);//Delete                            if(firing) {if(Index <=firinglength) {Firinglength--; }                                if(Index <=Firingindex) {Firingindex--;                }                            }                        }                    }); }                return  This; }, has:function(FN) {returnFn? Jquery.inarray (FN, list) >-1:!! (List &&list.length); }, Empty:function() {list= []; Firinglength= 0; return  This; }, disable:function() {list= Stack = Memory =undefined; return  This; }, Disabled:function() {                return!list; }, Lock:function() {stack=undefined; if( !memory)                {self.disable (); }                return  This; }, Locked:function() {                return!Stack; }, Firewith:function(context, args) {if(List && (!fired | |stack)) {args= Args | | []; Args= [Context, Args.slice?]args.slice (): args]; if(firing) {Stack.push (args); } Else{fire (args); }                }                return  This; }, Fire:function() {Self.firewith ( This, arguments);//cb.fire (' hello ');                return  This; }, fired:function() {                return!!fired;    }        }; returnSelf ;};

jquery Source---(2880, 3042) Callbacks: Callback object: Unified management of functions

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.