JavaScript design mode-subscription publishing mode (viewer mode)

Source: Internet
Author: User

varEvent = (function() {            varGlobal = This, Event, _default= ' Default '; Event=function() {                var_create, _listen, _trigger, _remove, _shi FT=Array.prototype.shift, _unshift=Array.prototype.unshift, Namespacecache={}, each=function(ary, fn) {varret;  for(vari = 0, L = ary.length; I < L; i + +) {                            varn =Ary[i]; RET=Fn.call (n, I, n);                        }; returnret;                }; _listen=function(Key, FN, cache) {if(!Cache[key]) {Cache[key]= [];                    };                Cache[key].push (FN);                }; _trigger=function() {                    varCache =_shift.call (arguments), key=_shift.call (arguments), args=arguments, _self= This, Stack=Cache[key]; if(!stack | |!stack.length)return; returnEach (Stack,function() {                        return  This. Apply (_self, args);                });                }; _remove=function(key, Cache, fn) {if(Cache[key]) {if(FN) { for(vari = cache[key].length; I >= 0; i--) {                                if(Cache[key][i] = = =fn) {Cache[key].splice (i,1);                            };                        }; } Else{Cache[key]= [];                    };                };                }; _create=function(namespace) {varNamespace = Namespace | |_default; varCache ={}, Offlinestack=[], ret={listen:function(Key, FN, last) {_listen (key, FN, cache); if(Offlinestack = = =NULL)return; if(last = = = ' last ')) {Offlinestack.length&&Offlinestack.pop () (); } Else{each (Offlinestack,function() {                                         This();                                });                                }; Offlinestack=NULL; }, Trigger:function() {                                varFN, args, _self= This;                                _unshift.call (arguments, cache); Args=arguments; FN=function() {                                    return_trigger.apply (_self, args);                                }; if(offlinestack) {returnOfflinestack.push (FN);                                }; returnfn (); }, remove:function(Key, FN) {_remove (key, Cache, FN); }, one:function(Key, FN, last) {_remove (key, cache);  This. Listen (key, FN, last);                    }                        }; returnNamespace? (Namespacecache[namespace]? Namespacecache[namespace]: namespacecache[namespace] =ret): ret;                }; return{create: _create, one:function(Key, FN, last) {varevent = This. Create ();                    Event.one (Key, FN, last); }, remove:function(Key, FN) {varevent = This. Create ();                    Event.remove (key, FN); }, listen:function(Key, FN, last) {varevent = This. Create ();                    Event.listen (Key, FN, last); }, Trigger:function() {                        varevent = This. Create (); Event.trigger.apply ( This, arguments);            }                };            }(); returnEvent; })();

Use posture:

        /*//Pre-Post subscription Event.trigger (' Evt1 ', 1, 2);    Event.trigger (' Evt1 ', 3, 4);    Are saved in the Offlinestack to Event.listen (' EVT1 ', E1); When there is listen monitoring, traverse Offlinestack method, send to the first listen Event.listen (' EVT1 ', E2);*/        /*//Pre-subscription post Event.listen (' EVT1 ', E1);    Event.listen (' EVT1 ', E2);    The pre-subscribed events are stored in the cache object Event.trigger (' Evt1 ', 1, 2);   Each publication iterates through an array of object event names in the cache object Event.trigger (' Evt1 ', 3, 4); */        /*//First post Post subscription Listen method The third parameter can be last, only to go to the final Event.trigger (' Evt1 ', 1, 2) in more than one trigger;        1).    Event.trigger (' Evt1 ', 3, 4); 2).    Are saved in the Offlinestack to Event.listen (' EVT1 ', E1, ' last '); will only receive 2). This trigger*/        /*//Subscribe to post and then delete and then publish, you will find the Evt1 event object Cache[key] Array less e1 function, so//re-release only E2 executed Event.listen (' EVT1 ', E1);           Event.listen (' EVT1 ', E2);           Event.trigger (' Evt1 ', 1, 2);        Event.remove (' EVT1 ', E1); Event.trigger (' Evt1 ', 3, 4);*/        //subscribe more and use only one subscription        /*//1).        Subscribe first post Event.one (' EVT1 ', E1);    Event.one (' EVT1 ', E2);    will use this because the previous one was deleted Event.trigger (' Evt1 ', 11, 22); So the E2 function will be executed two times Event.trigger (' evt1 ',;*/        //2). Post-Release subscription        /*Event.trigger (' evt1 ', 11, 22);        So it executes two times E2 function Event.trigger (' evt1 ', 33, 44);    Event.one (' EVT1 ', E1);    This will be used because Offlinestack is set to null event.one (' EVT1 ', E2); This does not execute, need to wait until the next trigger will be triggered, because E1 deleted from the cache, added E2, if there is another one method, and so on, will delete the last listener function, add a new listener function*/        //3). The third parameter of subscribing to the one method after first posting will only receive the last triggerEvent.trigger (' evt1 ', 11, 22); Event.trigger (' Evt1 ', 33, 44); Event.one (' Evt1 ', E2, ' last '); Event.one (' Evt1 ', E1, ' last ');

JavaScript design mode-subscription publishing mode (viewer mode)

Related Article

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.