EventBus.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-app-EventBus'>/**
  19. </span> * @class Ext.app.EventBus
  20. * @private
  21. */
  22. Ext.define('Ext.app.EventBus', {
  23. requires: [
  24. 'Ext.util.Event',
  25. 'Ext.Component'
  26. ],
  27. mixins: {
  28. observable: 'Ext.util.Observable'
  29. },
  30. constructor: function() {
  31. this.mixins.observable.constructor.call(this);
  32. this.bus = {};
  33. var me = this;
  34. Ext.override(Ext.Component, {
  35. fireEvent: function(ev) {
  36. if (Ext.util.Observable.prototype.fireEvent.apply(this, arguments) !== false) {
  37. return me.dispatch.call(me, ev, this, arguments);
  38. }
  39. return false;
  40. }
  41. });
  42. },
  43. dispatch: function(ev, target, args) {
  44. var bus = this.bus,
  45. selectors = bus[ev],
  46. selector, controllers, id, events, event, i, ln;
  47. if (selectors) {
  48. // Loop over all the selectors that are bound to this event
  49. for (selector in selectors) {
  50. // Check if the target matches the selector
  51. if (selectors.hasOwnProperty(selector) &amp;&amp; target.is(selector)) {
  52. // Loop over all the controllers that are bound to this selector
  53. controllers = selectors[selector];
  54. for (id in controllers) {
  55. if (controllers.hasOwnProperty(id)) {
  56. // Loop over all the events that are bound to this selector on this controller
  57. events = controllers[id];
  58. for (i = 0, ln = events.length; i &lt; ln; i++) {
  59. event = events[i];
  60. // Fire the event!
  61. if (event.fire.apply(event, Array.prototype.slice.call(args, 1)) === false) {
  62. return false;
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }
  70. return true;
  71. },
  72. control: function(selectors, listeners, controller) {
  73. var bus = this.bus,
  74. hasListeners, tree, list,
  75. selector, options, listener, scope, event, listenerList, ev;
  76. if (Ext.isString(selectors)) {
  77. selector = selectors;
  78. selectors = {};
  79. selectors[selector] = listeners;
  80. this.control(selectors, null, controller);
  81. return;
  82. }
  83. hasListeners = Ext.util.Observable.HasListeners.prototype;
  84. for (selector in selectors) {
  85. if (selectors.hasOwnProperty(selector)) {
  86. listenerList = selectors[selector] || {};
  87. for (ev in listenerList) {
  88. if (listenerList.hasOwnProperty(ev)) {
  89. options = {};
  90. listener = listenerList[ev];
  91. scope = controller;
  92. event = new Ext.util.Event(controller, ev);
  93. // Normalize the listener
  94. if (Ext.isObject(listener)) {
  95. options = listener;
  96. listener = options.fn;
  97. scope = options.scope || controller;
  98. delete options.fn;
  99. delete options.scope;
  100. }
  101. event.addListener(listener, scope, options);
  102. hasListeners[ev] = 1;
  103. // Create the bus tree if it is not there yet
  104. tree = bus[ev] || (bus[ev] = {});
  105. tree = tree[selector] || (tree[selector] = {});
  106. list = tree[controller.id] || (tree[controller.id] = []);
  107. // Push our listener in our bus
  108. list.push(event);
  109. }
  110. } //end inner loop
  111. }
  112. } //end outer loop
  113. }
  114. });
  115. </pre>
  116. </body>
  117. </html>