Mediator pattern (HB Stone)


/ Published in: JavaScript
Save to your folder(s)

Somewhat tweaked


Copy this code and paste it in your HTML
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. <meta name="description" content="">
  7. <script>
  8. Mediator = function () {
  9. var debug = function (m) {
  10. console.info(m)
  11. },
  12.  
  13. components = {},
  14.  
  15. addComponent = function (name, component, replaceDuplicate) {
  16. if (name in components) {
  17. if (replaceDuplicate) {
  18. removeComponent(name);
  19. }
  20. else {
  21. throw new Error('Mediator name conflict: ' + name);
  22. }
  23. }
  24. components[name] = component;
  25. },
  26.  
  27. broadcast = function (event, args, source) {
  28. var c, s,
  29. e = event || false,
  30. a = args || [];
  31.  
  32. if (!e) { return; }
  33.  
  34. for (c in components) {
  35. if (typeof components[c]["on" + e] == "function") {
  36. try {
  37. s = source || components[c];
  38. components[c]["on" + e].apply(s, a);
  39. }
  40. catch (err) {
  41. debug(["Mediator error.", e, a, s, err].join(' '));
  42. }ac
  43. }
  44. }
  45. },
  46. removeComponent = function (name) {
  47. if (name in components) { delete components[name]; }
  48. },
  49. getComponent = function (name) { return components[name] || false; },
  50. contains = function (name) { return (name in components); };
  51.  
  52. return {
  53. name: "Mediator",
  54. broadcast: broadcast,
  55. add: addComponent,
  56. rem: removeComponent,
  57. get: getComponent,
  58. has: contains
  59. };
  60. }();
  61.  
  62. Mediator.add('TestObject', function () {
  63. var someNumber = 0, // sample variable
  64. someString = 'another sample variable';
  65.  
  66. return {
  67. onInitialize: function () {
  68. console.debug(this)
  69. // this.name is automatically assigned by the Mediator
  70. console.log(this.name + " initialized.");
  71. },
  72. onFakeEvent: function () {
  73. someNumber++;
  74. console.log("Handled " + someNumber + " times!");
  75. },
  76. onSetString: function (str) {
  77. someString = str;
  78. console.log('Assigned ' + someString);
  79. }
  80. }
  81. }());
  82.  
  83. </script>
  84.  
  85. </head>
  86. <body>
  87.  
  88. <script>
  89. Mediator.broadcast("Initialize"); // alerts "TestObject initialized"
  90. Mediator.broadcast('FakeEvent'); // alerts "Handled 1 times!" (I know, bad grammar)
  91. Mediator.broadcast('SetString', ['test string']); // alerts "Assigned test string"
  92. Mediator.broadcast('FakeEvent'); // alerts "Handled 2 times!"
  93. Mediator.broadcast('SessionStart'); // this call is safely ignored
  94. Mediator.broadcast('Translate', ['this is also safely ignored']);
  95. </script>
  96.  
  97. </body>
  98. </html>

URL: http://arguments.callee.info/2009/05/18/javascript-design-patterns--mediator/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.