1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-util.Observable'>/**
2 </span> * @class Ext.util.Observable
3 * Base class that provides a common interface for publishing events. Subclasses are expected to
4 * to have a property "events" with all the events defined, and, optionally, a property "listeners"
5 * with configured listeners defined.<br>
7 * <pre><code>
8 Employee = Ext.extend(Ext.util.Observable, {
9 constructor: function(config){
10 this.name = config.name;
12 "fired" : true,
13 "quit" : true
16 // Copy configured listeners into *this* object so that the base class&#39;s
17 // constructor will add them.
18 this.listeners = config.listeners;
20 // Call our superclass constructor to complete construction process.
21 Employee.superclass.constructor.call(this, config)
24 </code></pre>
25 * This could then be used like this:<pre><code>
26 var newEmployee = new Employee({
30 // By default, "this" will be the object that fired the event.
31 alert(this.name + " has quit!");
35 </code></pre>
38 Ext.define('Ext.util.Observable', {
40 /* Begin Definitions */
42 requires: ['Ext.util.Event'],
45 <span id='Ext-util.Observable-method-releaseCapture'> /**
46 </span> * Removes <b>all</b> added captures from the Observable.
47 * @param {Observable} o The Observable to release
50 releaseCapture: function(o) {
51 o.fireEvent = this.prototype.fireEvent;
54 <span id='Ext-util.Observable-method-capture'> /**
55 </span> * Starts capture on the specified Observable. All events will be passed
56 * to the supplied function with the event name + standard signature of the event
57 * <b>before</b> the event is fired. If the supplied function returns false,
58 * the event will not fire.
59 * @param {Observable} o The Observable to capture events from.
60 * @param {Function} fn The function to call when an event is fired.
61 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the Observable firing the event.
64 capture: function(o, fn, scope) {
65 o.fireEvent = Ext.Function.createInterceptor(o.fireEvent, fn, scope);
68 <span id='Ext-util.Observable-method-observe'> /**
69 </span>Sets observability on the passed class constructor.
71 This makes any event fired on any instance of the passed class also fire a single event through
72 the __class__ allowing for central handling of events on many instances at once.
76 Ext.util.Observable.observe(Ext.data.Connection);
77 Ext.data.Connection.on('beforerequest', function(con, options) {
78 console.log('Ajax request made to ' + options.url);
81 * @param {Function} c The class constructor to make observable.
82 * @param {Object} listeners An object containing a series of listeners to add. See {@link #addListener}.
86 observe: function(cls, listeners) {
88 if (!cls.isObservable) {
89 Ext.applyIf(cls, new this());
90 this.capture(cls.prototype, cls.fireEvent, cls);
92 if (Ext.isObject(listeners)) {
100 /* End Definitions */
102 <span id='Ext-util.Observable-cfg-listeners'> /**
103 </span> * @cfg {Object} listeners (optional) <p>A config object containing one or more event handlers to be added to this
104 * object during initialization. This should be a valid listeners config object as specified in the
105 * {@link #addListener} example for attaching multiple handlers at once.</p>
106 * <br><p><b><u>DOM events from ExtJs {@link Ext.Component Components}</u></b></p>
107 * <br><p>While <i>some</i> ExtJs Component classes export selected DOM events (e.g. "click", "mouseover" etc), this
108 * is usually only done when extra value can be added. For example the {@link Ext.view.View DataView}'s
109 * <b><code>{@link Ext.view.View#click click}</code></b> event passing the node clicked on. To access DOM
110 * events directly from a child element of a Component, we need to specify the <code>element</code> option to
111 * identify the Component property to add a DOM listener to:
112 * <pre><code>
113 new Ext.panel.Panel({
121 element: 'el', //bind to the underlying el property on the panel
122 fn: function(){ console.log('click el'); }
125 element: 'body', //bind to the underlying body property on the panel
126 fn: function(){ console.log('dblclick body'); }
130 </code></pre>
136 constructor: function(config) {
139 Ext.apply(me, config);
144 me.events = me.events || {};
146 if (me.bubbleEvents) {
147 me.enableBubble(me.bubbleEvents);
152 eventOptionsRe : /^(?:scope|delay|buffer|single|stopEvent|preventDefault|stopPropagation|normalized|args|delegate|element|vertical|horizontal)$/,
154 <span id='Ext-util.Observable-method-addManagedListener'> /**
155 </span> * <p>Adds listeners to any Observable object (or Element) which are automatically removed when this Component
157 * @param {Observable/Element} item The item to which to add a listener/listeners.
158 * @param {Object/String} ename The event name, or an object containing event name properties.
159 * @param {Function} fn Optional. If the <code>ename</code> parameter was an event name, this
160 * is the handler function.
161 * @param {Object} scope Optional. If the <code>ename</code> parameter was an event name, this
162 * is the scope (<code>this</code> reference) in which the handler function is executed.
163 * @param {Object} opt Optional. If the <code>ename</code> parameter was an event name, this
164 * is the {@link Ext.util.Observable#addListener addListener} options.
166 addManagedListener : function(item, ename, fn, scope, options) {
168 managedListeners = me.managedListeners = me.managedListeners || [],
171 if (Ext.isObject(ename)) {
173 for (ename in options) {
174 if (options.hasOwnProperty(ename)) {
175 config = options[ename];
176 if (!me.eventOptionsRe.test(ename)) {
177 me.addManagedListener(item, ename, config.fn || config, config.scope || options.scope, config.fn ? config : options);
183 managedListeners.push({
191 item.on(ename, fn, scope, options);
195 <span id='Ext-util.Observable-method-removeManagedListener'> /**
196 </span> * Removes listeners that were added by the {@link #mon} method.
197 * @param {Observable|Element} item The item from which to remove a listener/listeners.
198 * @param {Object|String} ename The event name, or an object containing event name properties.
199 * @param {Function} fn Optional. If the <code>ename</code> parameter was an event name, this
200 * is the handler function.
201 * @param {Object} scope Optional. If the <code>ename</code> parameter was an event name, this
202 * is the scope (<code>this</code> reference) in which the handler function is executed.
204 removeManagedListener : function(item, ename, fn, scope) {
213 if (Ext.isObject(ename)) {
215 for (ename in options) {
216 if (options.hasOwnProperty(ename)) {
217 config = options[ename];
218 if (!me.eventOptionsRe.test(ename)) {
219 me.removeManagedListener(item, ename, config.fn || config, config.scope || options.scope);
225 managedListeners = me.managedListeners ? me.managedListeners.slice() : [];
226 length = managedListeners.length;
228 for (i = 0; i < length; i++) {
229 managedListener = managedListeners[i];
230 if (managedListener.item === item && managedListener.ename === ename && (!fn || managedListener.fn === fn) && (!scope || managedListener.scope === scope)) {
231 Ext.Array.remove(me.managedListeners, managedListener);
232 item.un(managedListener.ename, managedListener.fn, managedListener.scope);
237 <span id='Ext-util.Observable-method-fireEvent'> /**
238 </span> * <p>Fires the specified event with the passed parameters (minus the event name).</p>
239 * <p>An event may be set to bubble up an Observable parent hierarchy (See {@link Ext.Component#getBubbleTarget})
240 * by calling {@link #enableBubble}.</p>
241 * @param {String} eventName The name of the event to fire.
242 * @param {Object...} args Variable number of parameters are passed to handlers.
243 * @return {Boolean} returns false if any of the handlers return false otherwise it returns true.
245 fireEvent: function() {
247 args = Ext.Array.toArray(arguments),
248 ename = args[0].toLowerCase(),
250 event = me.events[ename],
251 queue = me.eventQueue,
254 if (me.eventsSuspended === true) {
258 } else if (event && Ext.isObject(event) && event.bubble) {
259 if (event.fire.apply(event, args.slice(1)) === false) {
262 parent = me.getBubbleTarget && me.getBubbleTarget();
263 if (parent && parent.isObservable) {
264 if (!parent.events[ename] || !Ext.isObject(parent.events[ename]) || !parent.events[ename].bubble) {
265 parent.enableBubble(ename);
267 return parent.fireEvent.apply(parent, args);
269 } else if (event && Ext.isObject(event)) {
271 ret = event.fire.apply(event, args);
276 <span id='Ext-util.Observable-method-addListener'> /**
277 </span> * Appends an event handler to this object.
278 * @param {String} eventName The name of the event to listen for. May also be an object who's property names are event names. See
279 * @param {Function} handler The method the event invokes.
280 * @param {Object} scope (optional) The scope (<code><b>this</b></code> reference) in which the handler function is executed.
281 * <b>If omitted, defaults to the object which fired the event.</b>
282 * @param {Object} options (optional) An object containing handler configuration.
283 * properties. This may contain any of the following properties:<ul>
284 * <li><b>scope</b> : Object<div class="sub-desc">The scope (<code><b>this</b></code> reference) in which the handler function is executed.
285 * <b>If omitted, defaults to the object which fired the event.</b></div></li>
286 * <li><b>delay</b> : Number<div class="sub-desc">The number of milliseconds to delay the invocation of the handler after the event fires.</div></li>
287 * <li><b>single</b> : Boolean<div class="sub-desc">True to add a handler to handle just the next firing of the event, and then remove itself.</div></li>
288 * <li><b>buffer</b> : Number<div class="sub-desc">Causes the handler to be scheduled to run in an {@link Ext.util.DelayedTask} delayed
289 * by the specified number of milliseconds. If the event fires again within that time, the original
290 * handler is <em>not</em> invoked, but the new handler is scheduled in its place.</div></li>
291 * <li><b>target</b> : Observable<div class="sub-desc">Only call the handler if the event was fired on the target Observable, <i>not</i>
292 * if the event was bubbled up from a child Observable.</div></li>
293 * <li><b>element</b> : String<div class="sub-desc"><b>This option is only valid for listeners bound to {@link Ext.Component Components}.</b>
294 * The name of a Component property which references an element to add a listener to.
295 * <p>This option is useful during Component construction to add DOM event listeners to elements of {@link Ext.Component Components} which
296 * will exist only after the Component is rendered. For example, to add a click listener to a Panel's body:<pre><code>
297 new Ext.panel.Panel({
300 click: this.handlePanelClick,
304 </code></pre></p>
305 * <p>When added in this way, the options available are the options applicable to {@link Ext.core.Element#addListener}</p></div></li>
306 * </ul><br>
308 * <b>Combining Options</b><br>
309 * Using the options argument, it is possible to combine different types of listeners:<br>
311 * A delayed, one-time listener.
312 * <pre><code>
313 myPanel.on('hide', this.handleClick, this, {
316 });</code></pre>
318 * <b>Attaching multiple handlers in 1 call</b><br>
319 * The method also allows for a single argument to be passed which is a config object containing properties
320 * which specify multiple events. For example:<pre><code>
322 cellClick: this.onCellClick,
323 mouseover: this.onMouseOver,
324 mouseout: this.onMouseOut,
325 scope: this // Important. Ensure "this" is correct during handler execution
327 </code></pre>.
330 addListener: function(ename, fn, scope, options) {
335 if (Ext.isObject(ename)) {
337 for (ename in options) {
338 if (options.hasOwnProperty(ename)) {
339 config = options[ename];
340 if (!me.eventOptionsRe.test(ename)) {
341 me.addListener(ename, config.fn || config, config.scope || options.scope, config.fn ? config : options);
347 ename = ename.toLowerCase();
348 me.events[ename] = me.events[ename] || true;
349 event = me.events[ename] || true;
350 if (Ext.isBoolean(event)) {
351 me.events[ename] = event = new Ext.util.Event(me, ename);
353 event.addListener(fn, scope, Ext.isObject(options) ? options : {});
357 <span id='Ext-util.Observable-method-removeListener'> /**
358 </span> * Removes an event handler.
359 * @param {String} eventName The type of event the handler was associated with.
360 * @param {Function} handler The handler to remove. <b>This must be a reference to the function passed into the {@link #addListener} call.</b>
361 * @param {Object} scope (optional) The scope originally specified for the handler.
363 removeListener: function(ename, fn, scope) {
369 if (Ext.isObject(ename)) {
371 for (ename in options) {
372 if (options.hasOwnProperty(ename)) {
373 config = options[ename];
374 if (!me.eventOptionsRe.test(ename)) {
375 me.removeListener(ename, config.fn || config, config.scope || options.scope);
380 ename = ename.toLowerCase();
381 event = me.events[ename];
383 event.removeListener(fn, scope);
388 <span id='Ext-util.Observable-method-clearListeners'> /**
389 </span> * Removes all listeners for this object including the managed listeners
391 clearListeners: function() {
392 var events = this.events,
396 for (key in events) {
397 if (events.hasOwnProperty(key)) {
400 event.clearListeners();
405 this.clearManagedListeners();
409 purgeListeners : function() {
410 console.warn('Observable: purgeListeners has been deprecated. Please use clearListeners.');
411 return this.clearListeners.apply(this, arguments);
415 <span id='Ext-util.Observable-method-clearManagedListeners'> /**
416 </span> * Removes all managed listeners for this object.
418 clearManagedListeners : function() {
419 var managedListeners = this.managedListeners || [],
421 len = managedListeners.length,
424 for (; i < len; i++) {
425 managedListener = managedListeners[i];
426 managedListener.item.un(managedListener.ename, managedListener.fn, managedListener.scope);
429 this.managedListeners = [];
433 purgeManagedListeners : function() {
434 console.warn('Observable: purgeManagedListeners has been deprecated. Please use clearManagedListeners.');
435 return this.clearManagedListeners.apply(this, arguments);
439 <span id='Ext-util.Observable-method-addEvents'> /**
440 </span> * Adds the specified events to the list of events which this Observable may fire.
441 * @param {Object/String} o Either an object with event names as properties with a value of <code>true</code>
442 * or the first event name string if multiple event names are being passed as separate parameters.
443 * @param {String} [additional] Optional additional event names if multiple event names are being passed as separate parameters.
444 * Usage:<pre><code>
445 this.addEvents('storeloaded', 'storecleared');
446 </code></pre>
448 addEvents: function(o) {
454 me.events = me.events || {};
455 if (Ext.isString(o)) {
460 me.events[args[i]] = me.events[args[i]] || true;
463 Ext.applyIf(me.events, o);
467 <span id='Ext-util.Observable-method-hasListener'> /**
468 </span> * Checks to see if this object has any listeners for a specified event
469 * @param {String} eventName The name of the event to check for
470 * @return {Boolean} True if the event is being listened for, else false
472 hasListener: function(ename) {
473 var event = this.events[ename.toLowerCase()];
474 return event && event.isEvent === true && event.listeners.length > 0;
477 <span id='Ext-util.Observable-method-suspendEvents'> /**
478 </span> * Suspend the firing of all events. (see {@link #resumeEvents})
479 * @param {Boolean} queueSuspended Pass as true to queue up suspended events to be fired
480 * after the {@link #resumeEvents} call instead of discarding all suspended events;
482 suspendEvents: function(queueSuspended) {
483 this.eventsSuspended = true;
484 if (queueSuspended && !this.eventQueue) {
485 this.eventQueue = [];
489 <span id='Ext-util.Observable-method-resumeEvents'> /**
490 </span> * Resume firing events. (see {@link #suspendEvents})
491 * If events were suspended using the <code><b>queueSuspended</b></code> parameter, then all
492 * events fired during event suspension will be sent to any listeners now.
494 resumeEvents: function() {
496 queued = me.eventQueue || [];
498 me.eventsSuspended = false;
499 delete me.eventQueue;
503 me.fireEvent.apply(me, e);
507 <span id='Ext-util.Observable-method-relayEvents'> /**
508 </span> * Relays selected events from the specified Observable as if the events were fired by <code><b>this</b></code>.
509 * @param {Object} origin The Observable whose events this object is to relay.
510 * @param {Array} events Array of event names to relay.
512 relayEvents : function(origin, events, prefix) {
513 prefix = prefix || '';
520 for (; i < len; i++) {
521 oldName = events[i].substr(prefix.length);
522 newName = prefix + oldName;
523 me.events[newName] = me.events[newName] || true;
524 origin.on(oldName, me.createRelayer(newName));
528 <span id='Ext-util.Observable-method-createRelayer'> /**
530 * Creates an event handling function which refires the event from this object as the passed event name.
532 * @returns {Function}
534 createRelayer: function(newName){
537 return me.fireEvent.apply(me, [newName].concat(Array.prototype.slice.call(arguments, 0, -1)));
541 <span id='Ext-util.Observable-method-enableBubble'> /**
542 </span> * <p>Enables events fired by this Observable to bubble up an owner hierarchy by calling
543 * <code>this.getBubbleTarget()</code> if present. There is no implementation in the Observable base class.</p>
544 * <p>This is commonly used by Ext.Components to bubble events to owner Containers. See {@link Ext.Component#getBubbleTarget}. The default
545 * implementation in Ext.Component returns the Component's immediate owner. But if a known target is required, this can be overridden to
546 * access the required target more quickly.</p>
547 * <p>Example:</p><pre><code>
548 Ext.override(Ext.form.field.Base, {
549 // Add functionality to Field&#39;s initComponent to enable the change event to bubble
550 initComponent : Ext.Function.createSequence(Ext.form.field.Base.prototype.initComponent, function() {
551 this.enableBubble('change');
554 // We know that we want Field&#39;s events to bubble directly to the FormPanel.
555 getBubbleTarget : function() {
556 if (!this.formPanel) {
557 this.formPanel = this.findParentByType('form');
559 return this.formPanel;
563 var myForm = new Ext.formPanel({
564 title: 'User Details',
570 // Title goes red if form has been modified.
571 myForm.header.setStyle('color', 'red');
575 </code></pre>
576 * @param {String/Array} events The event name to bubble, or an Array of event names.
578 enableBubble: function(events) {
580 if (!Ext.isEmpty(events)) {
581 events = Ext.isArray(events) ? events: Ext.Array.toArray(arguments);
584 ename = ename.toLowerCase();
585 var ce = me.events[ename] || true;
586 if (Ext.isBoolean(ce)) {
587 ce = new Ext.util.Event(me, ename);
588 me.events[ename] = ce;
595 <span id='Ext-util.Observable-method-un'> /**
596 </span> * Removes an event handler (shorthand for {@link #removeListener}.)
597 * @param {String} eventName The type of event the handler was associated with.
598 * @param {Function} handler The handler to remove. <b>This must be a reference to the function passed into the {@link #addListener} call.</b>
599 * @param {Object} scope (optional) The scope originally specified for the handler.
603 <span id='Ext-util.Observable-method-on'> /**
604 </span> * Appends an event handler to this object (shorthand for {@link #addListener}.)
605 * @param {String} eventName The type of event to listen for
606 * @param {Function} handler The method the event invokes
607 * @param {Object} scope (optional) The scope (<code><b>this</b></code> reference) in which the handler function is executed.
608 * <b>If omitted, defaults to the object which fired the event.</b>
609 * @param {Object} options (optional) An object containing handler configuration.
615 un: 'removeListener',
616 mon: 'addManagedListener',
617 mun: 'removeManagedListener'
620 //deprecated, will be removed in 5.0
621 this.observeClass = this.observe;
623 Ext.apply(Ext.util.Observable.prototype, function(){
624 // this is considered experimental (along with beforeMethod, afterMethod, removeMethodListener?)
625 // allows for easier interceptor and sequences, including cancelling and overwriting the return value of the call
627 function getMethodEvent(method){
628 var e = (this.methodEvents = this.methodEvents || {})[method],
635 this.methodEvents[method] = e = {};
636 e.originalFn = this[method];
637 e.methodName = method;
641 var makeCall = function(fn, scope, args){
642 if((v = fn.apply(scope || obj, args)) !== undefined){
643 if (typeof v == 'object') {
644 if(v.returnValue !== undefined){
645 returnValue = v.returnValue;
661 this[method] = function(){
662 var args = Array.prototype.slice.call(arguments, 0),
664 returnValue = v = undefined;
667 for(i = 0, len = e.before.length; i < len; i++){
669 makeCall(b.fn, b.scope, args);
675 if((v = e.originalFn.apply(obj, args)) !== undefined){
679 for(i = 0, len = e.after.length; i < len; i++){
681 makeCall(b.fn, b.scope, args);
693 // these are considered experimental
694 // allows for easier interceptor and sequences, including cancelling and overwriting the return value of the call
695 // adds an 'interceptor' called before the original method
696 beforeMethod : function(method, fn, scope){
697 getMethodEvent.call(this, method).before.push({
703 // adds a 'sequence' called after the original method
704 afterMethod : function(method, fn, scope){
705 getMethodEvent.call(this, method).after.push({
711 removeMethodListener: function(method, fn, scope){
712 var e = this.getMethodEvent(method),
714 for(i = 0, len = e.before.length; i < len; i++){
715 if(e.before[i].fn == fn && e.before[i].scope == scope){
716 e.before.splice(i, 1);
720 for(i = 0, len = e.after.length; i < len; i++){
721 if(e.after[i].fn == fn && e.after[i].scope == scope){
722 e.after.splice(i, 1);
728 toggleEventLogging: function(toggle) {
729 Ext.util.Observable[toggle ? 'capture' : 'releaseCapture'](this, function(en) {
730 if (Ext.isDefined(Ext.global.console)) {
731 Ext.global.console.log(en, arguments);
738 </pre></pre></body></html>