Upgrade to ExtJS 4.0.1 - Released 05/18/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                             return event.fire.apply(event, Array.prototype.slice.call(args, 1));
68                         }
69                     }
70                 }
71             }
72         }
73     },
74     
75     control: function(selectors, listeners, controller) {
76         var bus = this.bus,
77             selector, fn;
78         
79         if (Ext.isString(selectors)) {
80             selector = selectors;
81             selectors = {};
82             selectors[selector] = listeners;
83             this.control(selectors, null, controller);
84             return;
85         }
86     
87         Ext.Object.each(selectors, function(selector, listeners) {
88             Ext.Object.each(listeners, function(ev, listener) {
89                 var options = {},   
90                     scope = controller,
91                     event = Ext.create('Ext.util.Event', controller, ev);
92                 
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                 
102                 event.addListener(listener, scope, options);
103
104                 // Create the bus tree if it is not there yet
105                 bus[ev] = bus[ev] || {};
106                 bus[ev][selector] = bus[ev][selector] || {};
107                 bus[ev][selector][controller.id] = bus[ev][selector][controller.id] || [];            
108                 
109                 // Push our listener in our bus
110                 bus[ev][selector][controller.id].push(event);
111             });
112         });
113     }
114 });</pre>
115 </body>
116 </html>