PDA

View Full Version : Having a provider handle it's own custom events



azbok
16 Jun 2009, 5:37 PM
From the 3.0 RC2 docs for Ext.Direct:



{"type":"event","name":"message","data":"Successfully polled at: 11:19:30 am"}

// add a handler for a 'message' event sent by the server

Ext.Direct.on('message', function(e){
out.append(String.format('{0}', e.data));
out.el.scrollTo('t', 100000, true);
});
This is a bummer if you have lets say more than 1 provider each with their own message event but each for a different purpose. I'm not saying you should do that! Ideally you'd want to be able to listen on the specific provider for the custom events for that provider.



Ext.Direct.onProviderData = function(provider, e) {

if( Ext.isArray(e) ) {
for (var i = 0, len = e.length; i < len; i++) {
this.onProviderData(provider, e[i]);
}

return;
}

if ( e.name && e.name != 'event' && e.name != 'exception' ) {
this.fireEvent(e.name, e);
provider.fireEvent(e.name, e); // provider gets the specific event
} else if (e.type == 'exception'){
this.fireEvent('exception', e);
}

this.fireEvent('event', e, provider);
provider.fireEvent('event', e); // provider gets the general event
};
To listen for the message event you can do (note the console.log() function call which requires firebug)



provider = Ext.Direct.getProvider('my-provider-id');

provider.on({
data : function () { console.log('data', arguments); }, // standard event
exception : function () { console.log('exception, arguments); }, // note: this doesn't get called!
message : function () { console.log('message', arguments); }, // new event!
event : function () { console.log('event', arguments); } // new event!
});
About the exception part, the Ext application as a whole should probably have a generalized exception handler. That would be handled by using Ext.Direct.on() as usual, nothing has changed there.

evant
16 Jun 2009, 8:55 PM
This would be fairly useful.