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
/* Title: Using jQuery 1.7's $.Callbacks() feature
11
+
* Description: $.Callbacks are a multi-purpose callbacks list object which can be used as a base layer to build new functionality including simple publish/subscribe systems. We haven't yet released the API documentation for this feature just yet, more information on it (including lots of examples) here: http://addyosmani.com/blog/jquery-1-7s-callbacks-feature-demystified/.
12
+
*/
13
+
14
+
vartopics={};
15
+
16
+
jQuery.Topic=function(id){
17
+
varcallbacks,
18
+
topic=id&&topics[id];
19
+
if(!topic){
20
+
callbacks=jQuery.Callbacks();
21
+
topic={
22
+
publish: callbacks.fire,
23
+
subscribe: callbacks.add,
24
+
unsubscribe: callbacks.remove
25
+
};
26
+
if(id){
27
+
topics[id]=topic;
28
+
}
29
+
}
30
+
returntopic;
31
+
};
32
+
33
+
34
+
// Usage:
35
+
// Subscribers
36
+
$.Topic('mailArrived').subscribe(fn1);
37
+
$.Topic('mailArrived').subscribe(fn2);
38
+
$.Topic('mailSent').subscribe(fn1);
39
+
40
+
// Publisher
41
+
$.Topic('mailArrived').publish('hello world!');
42
+
$.Topic('mailSent').publish('woo! mail!');
43
+
44
+
// Here, 'hello world!' gets pushed to fn1 and fn2
45
+
// when the 'mailArrived' notification is published
46
+
// with 'woo! mail!' also being pushed to fn1 when
* Description: Using jQuery UI $.Observable (which is currently still under development), when objects/collections of data are changed or updated, events are triggered to inform any observers of the change
18
+
*/
19
+
20
+
/*$.observers example by @addyosmani*/
21
+
22
+
varmyData=[],
23
+
observer=$.observer(myData);
24
+
25
+
functiondataChange(data){
26
+
console.log('New data arrived with ID '+data[0].id+' and value '+data[0].title);
0 commit comments