Fuzzy Tolerance

Screencast #13 - PubSub

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<!doctype html>

<html lang="en">

<head>
  <meta charset="utf-8">
  <style>
      #container { width: 500px; margin: 0 auto; text-align: center; }
      #crapcatcher { border: 2px solid black; min-height: 300px; border-radius: 10px; padding: 10px; }
  </style>
</head>

<body>
  <div id="container">
      <h2>Crap Goes Here</h2>
      <div id="crapcatcher"></div>
      <button onclick="$('#crapcatcher').empty()">Clear the Catcher</button>
  </div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<script>
/* jQuery Tiny Pub/Sub - v0.7 - 10/27/2011
 * http://benalman.com/
 * Copyright (c) 2011 "Cowboy" Ben Alman; Licensed MIT, GPL */
(function($) {

  var o = $({});

  $.subscribe = function() {
    o.on.apply(o, arguments);
  };

  $.unsubscribe = function() {
    o.off.apply(o, arguments);
  };

  $.publish = function() {
    o.trigger.apply(o, arguments);
  };

}(jQuery));


// Examples
// $.subscribe("/some/topic", handler1);
// $.publish("/some/topic", [ "a", "b", "c" ]);
// $.unsubscribe("/some/topic", handler1);

function handler1(e) {
  $("#crapcatcher").append("Hello from Hander 1<br />");
}

function handler2(e, a) {
  $("#crapcatcher").append("Hello from Hander 2 - " + a + "<br />");
}

function handler3(e, a, b) {
  $("#crapcatcher").append("Hello from Hander 3 - " + a + ":" + b + "<br />");
}


              
</script>

</html>

Resources

Cowboy Ben Alman’s jQuery PubSub

Comments