Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / docs / source / EventBus.html
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     ],
26     mixins: {
27         observable: 'Ext.util.Observable'
28     },
29
30     constructor: function() {
31         this.mixins.observable.constructor.call(this);
32
33         this.bus = {};
34
35         var me = this;
36         Ext.override(Ext.Component, {
37             fireEvent: function(ev) {
38                 if (Ext.util.Observable.prototype.fireEvent.apply(this, arguments) !== false) {
39                     return me.dispatch.call(me, ev, this, arguments);
40                 }
41                 return false;
42             }
43         });
44     },
45
46     dispatch: function(ev, target, args) {
47         var bus = this.bus,
48             selectors = bus[ev],
49             selector, controllers, id, events, event, i, ln;
50
51         if (selectors) {
52             // Loop over all the selectors that are bound to this event
53             for (selector in selectors) {
54                 // Check if the target matches the selector
55                 if (target.is(selector)) {
56                     // Loop over all the controllers that are bound to this selector
57                     controllers = selectors[selector];
58                     for (id in controllers) {
59                         // Loop over all the events that are bound to this selector on this controller
60                         events = controllers[id];
61                         for (i = 0, ln = events.length; i &lt; ln; i++) {
62                             event = events[i];
63                             // Fire the event!
64                             if (event.fire.apply(event, Array.prototype.slice.call(args, 1)) === false) {
65                                 return false;
66                             };
67                         }
68                     }
69                 }
70             }
71         }
72     },
73
74     control: function(selectors, listeners, controller) {
75         var bus = this.bus,
76             selector, fn;
77
78         if (Ext.isString(selectors)) {
79             selector = selectors;
80             selectors = {};
81             selectors[selector] = listeners;
82             this.control(selectors, null, controller);
83             return;
84         }
85
86         Ext.Object.each(selectors, function(selector, listeners) {
87             Ext.Object.each(listeners, function(ev, listener) {
88                 var options = {},
89                     scope = controller,
90                     event = Ext.create('Ext.util.Event', controller, ev);
91
92                 // Normalize the listener
93                 if (Ext.isObject(listener)) {
94                     options = listener;
95                     listener = options.fn;
96                     scope = options.scope || controller;
97                     delete options.fn;
98                     delete options.scope;
99                 }
100
101                 event.addListener(listener, scope, options);
102
103                 // Create the bus tree if it is not there yet
104                 bus[ev] = bus[ev] || {};
105                 bus[ev][selector] = bus[ev][selector] || {};
106                 bus[ev][selector][controller.id] = bus[ev][selector][controller.id] || [];
107
108                 // Push our listener in our bus
109                 bus[ev][selector][controller.id].push(event);
110             });
111         });
112     }
113 });</pre>
114 </body>
115 </html>