PDA

View Full Version : Observable addEventTracers



chriswa
30 Apr 2008, 11:25 AM
UPDATE: Oops, I overlooked Ext.util.Observable.capture()!

I found it cumbersome to write out a bunch of obj.on(eventName, function(){ console.log('blah blah', arguments); }) handlers any time I wanted to see when events were fired, so I added a function to Ext.util.Observable.prototype to do it for me.

Now I can:


dataStore.addEventTracers( 'Application.ProblemDataSource.dataStore', [ 'load', 'loadexception', 'add', 'beforeload', 'clear', 'datachanged', 'remove', 'update' ] );

Which dumps out the following to my console:


(EventTracer) Application.ProblemDataSource.dataStore beforeload handler( [Object data=[0] baseParams=Object paramNames=Object, Object] )
(EventTracer) Application.ProblemDataSource.dataStore datachanged handler( [Object data=[8] baseParams=Object paramNames=Object] )
(EventTracer) Application.ProblemDataSource.dataStore load handler( [Object data=[8] baseParams=Object paramNames=Object, [Object id=0 data=Object json=[4] store=Object meta=Object, Object id=49 data=Object json=[4] store=Object meta=Object, Object id=57 data=Object json=[4] store=Object meta=Object, 5 more...], Object] )

Here's the function in all its inelegance, and its completely untested companion:


Ext.util.Observable.prototype.addEventTracers = function( objName, eventList ) {
function bark( objName, eventName ) {
console.log( '(EventTracer) ' + objName + ' ' + eventName + ' handler(', Array.prototype.slice.call(arguments, 2), ')' );
}
this.tracers = [];
for ( var i = 0; i < eventList.length; i++ ) {
var eventName = eventList[i];
var barker = bark.createDelegate( this, [ objName, eventName ], 0 );
this.on( eventName, barker );
this.tracers.push( [ eventName, barker ] );
}
};
Ext.util.Observable.prototype.removeEventTracers = function( objName, eventList ) {
for ( var i = 0; i < this.tracers.length; i++ ) {
this.removeListener( this.tracers[i][0], this.tracers[i][1] );
}
};

Hope someone finds this useful! :D

mystix
1 May 2008, 6:34 AM
did you perchance overlook these?
http://www.extjs.com/deploy/dev/docs/?class=Ext.util.Observable&member=Observable.capture
http://www.extjs.com/deploy/dev/docs/?class=Ext.util.Observable&member=Observable.releaseCapture

they capture / release all events on an Observable. does what you need with minimum fuss.

chriswa
1 May 2008, 8:56 AM
Hey, thanks! Indeed I did.

Looking up how capture() works, I've also discovered createInterceptor()! :D