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