PDA

View Full Version : Intercept of Ext.ComponentMgr.create to throw error on incorrect xtype



Nicolas BUI
14 Jan 2011, 2:19 AM
-- Updated : change post title suggested by devtig (thank you!)

While developping a complex interface, you are surely (as all of us) use lazy instanciation through xtype.
ex: {xtype: 'myComponent', myConf: 'something' }

When the xtype is not registered or if you mistyping (it happen sometime, huh?), ExtJS should throw an error when a xtype is not registered (maybe in the futur?). But no... you will get and exception that will not help you in your debug.

Yes I was in this case :)

So here a little snippet (an override) that will make something more readable and easier to debug.



/**
* Intercept the original componant instanciation, throw an exception if the xtype do not exists
*/
Ext.ComponentMgr.create = Ext.ComponentMgr.create.createInterceptor(function(config, defaultType) {
var type = config.xtype || defaultType;
if ( !Ext.ComponentMgr.isRegistered(type)) {
throw 'xtype ""'+type+'"" is not a registered component';
}
return true;
});

/**
* recreate the alias
*/
Ext.create = Ext.ComponentMgr.create;


Change the message as you want to make it clear.

Hoping this will help you a bit :).

Nicolas

devtig
14 Jan 2011, 2:39 AM
Nice override, but crap forum title. I suggest rename the title to: Intercept of Ext.ComponentMgr.create to throw error on incorrect xtype

Nicolas BUI
14 Jan 2011, 7:50 AM
I'm 100% agree with you about the title ... I had no inspiration about the title :)

devtig
14 Jan 2011, 7:55 AM
Go to Edit -> Go Advanced and change the title. The title just summarizes the inspiration you already had. You don't need new inspiration. You want people to read your post and they're not going to click on forum titles like this. (Not on a tech forum)