JQuery Source Callbacks

Source: Internet
Author: User

The Callbacks tool method, which functions as a unified management of function callbacks Jquery.callbacks = function (options) {} is used: Similar to event binding, after fire, the previously bound method is executed. Viewer mode.    function aaa () {alert (1);    } function bbb () {alert (2); } function CCC () {alert (3)} var cb = $.    Callbacks ();    Cb.add (AAA);    Cb.add (BBB);    Cb.add (CCC);    Cb.fire ();//popup 1 and 2, 3 Add (fun)-->list adding method. Fire to the list array for the For loop execution. /**********************************************************/function aaa () {alert (1);}    (function () {function bbb () {alert (2); }) (), AAA (), BBB ();//The Pop-up 1 and 2 cannot be performed at the same time because the unified scope of the pipe/**********************************************************/// But callbacks can fix this problem. var cb = $. Callbacks (); function aaa () {alert (1);}    Cb.add (AAA);(function () {function bbb () {alert (2); } cb.add (BBB);}) (); Cb.fire ();//Just one sentence is all you need. Trigger All//here as long as the callback object is global, you can add functions to the callback function at any time and then execute. /**********************************************************/jquery.callbacks = function (options) options configuration parameters. Four!!! *once:will Ensure the callback list can only is fired once (like a Deferred) * *memory:will keep track of previous values and would C All callback added *after the list have been fired right away with the latest ' memorized ' *values (like a Deferred) * * Unique:will ensure a callback can only is added once (no duplicate in the list) * *stoponfalse:interrupt callings when a C Allback The four parameters of returns Falsecallbacks. var cb = $. Callbacks (); Cb.fire (); Cb.fire ();//fire can perform multiple triggers. (1) Once: Representative, fire can only be triggered once. Cb.fire (); Cb.fire ();//Will not execute. Let the fire execute once. (2) Memory:var cb = $.    Callbacks ();    function aaa () {alert (1);    } cb.add (AAA);    Cb.fire ();    function bbb () {alert (2);   } cb.add (BBB); After this code executes, only fire out 1, cannot execute BBB function var cb = $. Callbacks (' memory '), function aaa () {alert (1);} Cb.add (AAA); Cb.fire (); function bbb () {alert (2);} Cb.add (BBB);//This code will print out 1 2. The following 2 can also be triggered, as long as the addition can be performed.    Memory is acting on top of add before calling fire. var cb = $.    Callbacks (' memory ');        function aaa () {alert (1); Return FalsE    } cb.add (AAA);    Cb.fire ();    function bbb () {alert (2);    } setTimeout (function () {cb.add (BBB); }, 1000); I think of an example that should be able to speak clearly, if the variable inside defines memory, as initialization, then it executes. Once you have called the fire and added the Add function to the inside, proceed directly. (3) Unique: To repeat, so that the function is unique. Acting on Addvar cb = $. Callbacks (); function aaa () {alert (1);} Cb.add (AAA); Cb.add (AAA); Cb.fire ();//This triggers 2 times var cb = $. Callbacks (' unique '), function aaa () {alert (1);} Cb.add (AAA); Cb.add (AAA); Cb.fire ();//Here Trigger 1 times, duplicate function name, cannot put into list, (4) Stoponfalsevar cb = $.    Callbacks (' stoponfalse '); function aaa () {alert (1); return false;} Cb.add (AAA), function BBB () {alert (2);} Cb.add (BBB); Cb.fire ();//Here only the alert (1) is executed. Instead of alert (2), there is no back execution//This variable acts on fire, determines whether the function returns false, and if it returns false, the list loop is immediately released. Of course, the form of combination can be accepted, for example, the form of memory and once combination, separated by the empty glyd. var cb = $. Callbacks (' once Stoponfalse '); Callbacks method: Add: The function, push into the list. Remove: Delete has detected whether there is this function, return false/trueempty empty array disable all locked. Prohibit, do not execute. Lock locked. Stack=undefinedlocked to determine whether to lock firewithfire:--"call is Firewith. --"Fire = function (data); fired determine if firEdlist = [], a list of saved functions is used. Add. Add:function () {if (list) {//First, we save the current lengthvar start = List.length; (function add (args) {//here supports incoming multiple A, Cb.add (AAA, BBB), Jquery.each (args, function (_, arg) {var type = Jquery.type (ARG), if (type = = = "function") {//judgment is not is the only. Unique!!! if (!options.unique | |!self.has (ARG)) {List.push (ARG);}} Supports array form, Cb.add ([AAA, BBB]), else if (Arg && arg.length && type!== "string") {//Inspect Recursivelyadd (arg);});}) (arguments);//Do we need to add the callbacks to the//current firing batch?if (firing) {firinglength = list.length;/ /merory is directly executed. } else if (memory) {Firingstart = Start;fire (memory);}} return this;}, support this: Cb.add (AAA); Cb.add (AAA, BBB), Cb.add ([AAA, BBB]); Remove: Delete. The array is traversed for deletion. Spliceremove:function () {if (list) {Jquery.each (arguments, function (_, arg) {var index;while (index = Jquery.inarr Ay (ARG, list, index)) >-1) {List.splice (index, 1);//Handle firing Indexesif (firing) {if (index<= firinglength) {firinglength--;} if (index <= firingindex) {firingindex--;}}});} Return This;},cb.add (AAA); Cb.add (BBB); Cb.add (CCC); Cb.remove (BBB); Cb.fire ();//Only AAA and Cccfire: Execute.  Fire = function (data) {memory = options.memory && data;fired = True;firingindex = Firingstart | | 0;firingstart = 0;firinglength = list.length;firing = true;//trigger start for (; list && Firingindex < firinglength; firingindex++) { if (list[firingindex].apply (data[0], data[1]) = = = False && options.stoponfalse) {memory = false;//to P Revent further calls using Addbreak;}} Firing = false;//Trigger End if (list) {//Judgment ListIf (stack) {if (stack.length) {//stack length is empty,//Call Fire (Stack.shift () recursively);//Will The first return in the stack. and Fire}}//If there is a memory, else if (memory) {//list empty lists = [];} else {self.disable ();}}},//call all callbacks with the given Contex T and argumentsfirewith:function (context, args) {if (list && (!fired | | | stack)) {args = args | | [];//If it is an array, it is converted to an object. args = [Context, ARGS.SLIce? Args.slice (): args];if (firing) {Stack.push (args);} else {fire (args);}} Return this;},//call all the callbacks with the given argumentsfire:function () {Self.firewith (this, arguments); return this;},//to know if the callbacks has already been called at least oncefired:function () {return!! Fired;} stack =!options.once && [],//here defines the time when a stack firing event is triggered. FireWith:fireWith:function (context, args) {if (list && (!fired | | | stack)) {args = args | | [];args = [Context, Args.slice args.slice (): args];//here, if it is firing, the fire is pressed into the stack if (firing) {Stack.push (args);} else {Fire (args);}} return this;}, there is an example of var cb = $. Callbacks (); var singal = True;function aaa () {alert (1); if (Singal) {cb.fire ();//stack.push (args); singal = false;}} Cb.add (AAA), function BBB () {alert (2);} Cb.add (BBB); Cb.fire (); the pop-up sequence is. This is achieved in the fire. The IF (list) {//Judgment ListIf (stack) {if (stack.length) {//stack's length is empty,//The Fire (Stack.shift ()) is called recursively, or the first one in the stack is returned. and Fire}}//If there is memory, empty the array. else if ( Memory) {//list empty lists = [];} else {self.disable ();}} 

JQuery Source Callbacks

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.