YouTip LogoYouTip

Misc Callbacks

[![Image 1: jQuery Misc Methods](#)jQuery Misc Methods](#) ## Example Add callback functions to $.Callbacks list $(function(){function fn1(value){alert(value); }function fn2(value){fn1("fn2 says: " + value); return false; }var callbacks = $.Callbacks(); callbacks.add(fn1); callbacks.fire("foo!"); callbacks.add(fn2); callbacks.fire("bar!"); }) [Try it yourself Β»](#) * * * ## Definition and Usage $.Callbacks() refers to a multi-purpose callback function list object, providing a powerful way to manage callback function queues. **Tip:** $.Callbacks is used internally by jQuery, such as providing basic functions for .ajax, $.Deferred and other components. It can also be used in some components with similar functionality, such as self-developed plugins. * * * ## Syntax _$_.Callbacks( flags ) | Parameter | Description | | --- | --- | | _flags_ | Optional. String type An optional list separated by spaces to change the behavior in the callback list | * * * ![Image 2: Example](#) ## More Examples The following is an example of using .remove() to remove a specific callback from the callback list ## Example $(function(){function fn1(value){alert(value); }function fn2(value){fn1("fn2 says: " + value); return false; }var callbacks = $.Callbacks(); callbacks.add(fn1); callbacks.fire("foo!"); callbacks.add(fn2); callbacks.fire("bar!"); callbacks.remove(fn2); callbacks.fire("foobar"); }) [Try it yourself Β»](#) * * * ## Supported Flags Parameters This flags parameter is an optional parameter of $.Callbacks(), structured as a space-separated optional list of flags to change the behavior in the callback list (e.g. $.Callbacks('unique stopOnFalse')). **The following are available flags:** | Parameter | Description | | --- | --- | | _once_ | Ensures the callback list is executed only once | | _memory_ | Caches the parameter value from the last fire, when add() adds a callback function, it immediately calls the newly added callback with the previous parameter value | | _unique_ | A callback can only be added once, no duplicate additions | | _stopOnFalse_ | After a callback function returns false, it interrupts subsequent callback functions | The following is an example of $.Callbacks("once") ## Example $(function(){function fn1(value){alert(value); }function fn2(value){fn1("fn2 says: " + value); return false; }var callbacks = $.Callbacks("once"); callbacks.add(fn1); callbacks.fire("foo"); callbacks.add(fn2); callbacks.fire("bar"); callbacks.remove(fn2); callbacks.fire("foobar"); }) [Try it yourself Β»](#) The following is an example of $.Callbacks("memory") ## Example $(function(){function fn1(value){alert(value); }function fn2(value){fn1("fn2 says: " + value); return false; }var callbacks = $.Callbacks("memory"); callbacks.add(fn1); callbacks.fire("foo"); callbacks.add(fn2); callbacks.fire("bar"); callbacks.remove(fn2); callbacks.fire("foobar"); }) [Try it yourself Β»](#) The following is an example of $.Callbacks("unique") ## Example $(function(){function fn1(value){alert(value); }function fn2(value){fn1("fn2 says: " + value); return false; }var callbacks = $.Callbacks("unique"); callbacks.add(fn1); callbacks.fire("foo"); callbacks.add(fn1); callbacks.add(fn2); callbacks.fire("bar"); callbacks.remove(fn2); callbacks.fire("foobar"); }) [Try it yourself Β»](#) The following is an example of $.Callbacks("stopOnFalse") ## Example $(function(){function fn1(value){alert(value); return false; }function fn2(value){fn1("fn2 says: " + value); return false; }var callbacks = $.Callbacks("stopOnFalse"); callbacks.add(fn1); callbacks.fire("foo"); callbacks.add(fn2); callbacks.fire("bar"); callbacks.remove(fn2); callbacks.fire("foobar"); }) [Try it yourself Β»](#) $.Callbacks() supports setting multiple flags for a list, not just one, which has a cumulative effect, similar to "&&". The following is an example of $.Callbacks('unique memory') ## Example $(function(){function fn1(value){alert(value); return false; }function fn2(value){fn1("fn2 says: " + value); return false; }var callbacks = $.Callbacks("unique memory"); callbacks.add(fn1); callbacks.fire("foo"); callbacks.add(fn1); callbacks.add(fn2); callbacks.fire("bar"); callbacks.add(fn2); callbacks.fire("baz"); callbacks.remove(fn2); callbacks.fire("foobar"); }) [Try it yourself Β»](#) $.Callbacks method can also be separated, for example: ## Example $(function(){function fn1(value){alert(value); }var callbacks = $.Callbacks(), add = callbacks.add, remove = callbacks.remove, fire = callbacks.fire; add(fn1); fire("hello world"); remove(fn1); }) [Try it yourself Β»](#) * * * ## $.Callbacks, $.Deferred and Pub/Sub The general idea behind pub/sub (observer pattern) is to promote loose coupling and efficient communication in applications. Observers are also known as subscribers, which point to the observed object. Observers (Publisher) notify users when events occur. As a demonstration of creating components with $.Callbacks(), using only the callback function list, a Pub/Sub system can be implemented. Using $.Callbacks as an article queue, you can implement article publishing and subscribing as follows: ## Example $(function(){function fn1(value){alert(value); return false; }function fn2(value){fn1("fn2 says: " + value); return false; }var topics = {}; jQuery.Topic = function(id){var callbacks, method, topic = id&&topics; if( !topic){callbacks = jQuery.Callbacks(); topic = {publish: callbacks.fire, subscribe: callbacks.add, unsubscribe: callbacks.remove}; if(id){topics = topic; }}return topic; }; $.Topic("mailArrived").subscribe(fn1); $.Topic("mailArrived").subscribe(fn2); $.Topic("mailSent").subscribe(fn1); $.Topic("mailArrived").publish("hello world!"); $.Topic("mailSent").publish("woo! mail!"); }) [Try it yourself Β»](#) Further improvement using $.Deferreds can ensure that publishers can only notify subscribers when a specific task is completed (or resolved). See the example code below: ## Example $(function(){function fn1(value){alert(value); return false; }function fn2(value){fn1("fn2 says: " + value
← Misc Callbacks EmptyJq Sel Target β†’