You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<desc>A multi-purpose callbacks list object that provides a powerful way to manage callback lists.</desc>
11
11
<longdesc>
12
12
<p>The <code>$.Callbacks()</code> function is internally used to provide the base functionality behind the jQuery <code>$.ajax()</code> and <code>$.Deferred()</code> components. It can be used as a similar base to define functionality for new components.</p>
13
-
<p><code>$.Callbacks()</code> support a number of methods including <code><ahref="/callbacks.add/">callbacks.add()</a></code>,<code><ahref="/callbacks.remove/">callbacks.remove()</a></code>, <code><ahref="/callbacks.fire/">callbacks.fire()</a></code> and <code><ahref="/callbacks.disable/">callbacks.disable()</a></code>.</p>
13
+
<p><code>$.Callbacks()</code> supports a number of methods including <code><ahref="/callbacks.add/">callbacks.add()</a></code>,<code><ahref="/callbacks.remove/">callbacks.remove()</a></code>, <code><ahref="/callbacks.fire/">callbacks.fire()</a></code> and <code><ahref="/callbacks.disable/">callbacks.disable()</a></code>.</p>
14
14
<h3id="getting-started">Getting started</h3>
15
15
<p>The following are two sample methods named <code>fn1</code> and <code>fn2</code>:</p>
16
16
<pre><code>
@@ -23,37 +23,45 @@ function fn2( value ){
23
23
return false;
24
24
}
25
25
</code></pre>
26
-
<p>These can be added as callbacks to a <code>$.Callbacks</code> list and invoked follows:</p>
26
+
<p>These can be added as callbacks to a <code>$.Callbacks</code> list and invoked as follows:</p>
<p>The result of this is that it becomes simple to construct complex lists of callbacks where input values can be passed through to as many functions as needed with ease.</p>
36
-
<p>Two specific methods were being used above: <code>.add()</code> and <code>.fire()</code>.add()supports adding new callbacks to the callback list, whilst fire() provides a way to pass arguments to be processed by the callbacks in the same list.</p>
37
-
<p>Another method supported by <code>$.Callbacks</code> is remove(), which has the ability to remove a particular callback from the callback list. Here"s a practical example of .remove() being used:</p>
40
+
<p>Two specific methods were being used above: <code>.add()</code> and <code>.fire()</code>. The <code>.add()</code> method supports adding new callbacks to the callback list, while the <code>.fire()</code> method executes the added functions and provides a way to pass arguments to be processed by the callbacks in the same list.</p>
41
+
<p>Another method supported by <code>$.Callbacks</code> is <code>.remove()</code>, which has the ability to remove a particular callback from the callback list. Here"s a practical example of <code>.remove()</code> being used:</p>
<p>The <code>flags</code> argument is an optional argument to <code>$.Callbacks()</code>, structured as a list of space-separated strings that change how the callback list behaves (eg. <code>$.Callbacks( 'unique stopOnFalse' )</code>).</p>
53
61
<h2>Possible flags:</h2>
54
62
<ul>
55
63
<li><code>once</code>: Ensures the callback list can only be fired once (like a Deferred).</li>
56
-
<li><code>memory</code>: Keep track of previous values and will call any callback added after the list has been fired right away with the latest "memorized" values (like a Deferred).</li>
64
+
<li><code>memory</code>: Keeps track of previous values and will call any callback added after the list has been fired right away with the latest "memorized" values (like a Deferred).</li>
57
65
<li><code>unique</code>: Ensures a callback can only be added once (so there are no duplicates in the list).</li>
58
66
<li><code>stopOnFalse</code>: Interrupts callings when a callback returns false.</li>
59
67
</ul>
@@ -137,7 +145,7 @@ bar
137
145
foobar
138
146
*/
139
147
</code></pre>
140
-
<p>Because $.Callbacks() supports a list of flags rather than just one, setting several flags has a cumulative effect similar to "&&". This means it's possible to combine flags to create callback lists that are say, both <i>unique</i> and <i>ensure if list was already fired, adding more callbacks will have it called with the latest fired value</i> (i.e. <code>$.Callbacks("unique memory")</code>).</p>
148
+
<p>Because <code>$.Callbacks()</code> supports a list of flags rather than just one, setting several flags has a cumulative effect similar to "&&". This means it's possible to combine flags to create callback lists that, say, both are <i>unique</i> and <i>ensure if list was already fired, adding more callbacks will have it called with the latest fired value</i> (i.e. <code>$.Callbacks("unique memory")</code>).</p>
<p>Flag combinations are internally used with <code>$.Callbacks()</code> in jQuery for the <code>.done()</code> and <code>.fail()</code> buckets on a Deferred - both of which use <code>$.Callbacks('memory once')</code>.</p>
176
-
<p>$.Callbacks methods can also be detached, should there be a need to define short-hand versions for convenience:</p>
183
+
<p>Flag combinations with <code>$.Callbacks()</code> are internally in jQuery for the <code>.done()</code> and <code>.fail()</code> functions on a Deferred — both of which use <code>$.Callbacks('memory once')</code>.</p>
184
+
<p>The methods of <code>$.Callbacks</code> can also be detached, should there be a need to define short-hand versions for convenience:</p>
177
185
<pre><code>
178
186
var callbacks = $.Callbacks(),
179
187
add = callbacks.add,
@@ -185,14 +193,15 @@ fire( "hello world");
185
193
remove( fn1 );
186
194
</code></pre>
187
195
<h3id="pubsub">$.Callbacks, $.Deferred and Pub/Sub</h3>
188
-
<p>The general idea behind pub/sub (the Observer pattern) is the promotion of loose coupling in applications. Rather than single objects calling on the methods of other objects, an object instead subscribes to a specific task or activity of another object and is notified when it occurs. Observers are also called Subscribers and we refer to the object being observed as the Publisher (or the subject). Publishers notify subscribers when events occur</p>
189
-
<p>As a demonstration of the component-creation capabilities of <code>$.Callbacks()</code>, it's possible to implement a Pub/Sub system using only callback lists. Using <code>$.Callbacks</code> as a topics queue, a system for publishing and subscribing to topics can be implemented as follows:</p>
196
+
<p>The general idea behind pub/sub (the Observer pattern) is the promotion of loose coupling in applications. Rather than single objects calling on the methods of other objects, an object instead subscribes to a specific task or activity of another object and is notified when it occurs. Observers are also called Subscribers, and we refer to the object being observed as the Publisher (or the subject). Publishers notify subscribers when events occur.</p>
197
+
<p>To demonstrate the component-creation capabilities of <code>$.Callbacks()</code>, it's possible to implement a Pub/Sub system using only callback lists. Using <code>$.Callbacks</code> as a topics queue, a system for publishing and subscribing to topics can be implemented as follows:</p>
190
198
<pre><code>var topics = {};
191
199
192
200
jQuery.Topic = function( id ) {
193
201
var callbacks,
194
202
method,
195
203
topic = id && topics[ id ];
204
+
196
205
if ( !topic ) {
197
206
callbacks = jQuery.Callbacks();
198
207
topic = {
@@ -254,4 +263,4 @@ dfd.resolve( "its been published!" );
0 commit comments