当给controller添加监听器(addListener)时,有时候会出现莫名其妙的错误:
比如 Cannot read property 'fn' of undefined,此时如果代码比较多的时候,不太好找错误,
此时可以用如下方法:
在 Ext.util.Observable中捕获错误的详细信息,添加红色代码即可,这样就可以找到错误原因了
addListener: function(ename, fn, scope, options) {
var me = this,config, event, hasListeners,
prevListenerCount = 0;
if (typeof ename !== 'string') {
options = ename;
for (ename in options) {
if (options.hasOwnProperty(ename)) {
config = options[ename];
if (!me.eventOptionsRe.test(ename)) {
if (typeof(config) == 'undefined') {
console.error('Cannot bind to event "' + ename + '" - method does not exist on object', options.scope);
} else {
me.addListener(ename, config.fn || config, config.scope || options.scope, config.fn ? config : options);
}
}
}
}
} else {
ename = ename.toLowerCase();
event = me.events[ename];
if (event && event.isEvent) {
prevListenerCount = event.listeners.length;
} else {
me.events[ename] = event = new Ext.util.Event(me, ename);
}
// Allow listeners: { click: 'onClick', scope: myObject }
if (typeof fn === 'string') {
//<debug>
if (!(scope[fn] || me[fn])) {
Ext.Error.raise('No method named "' + fn + '"');
}
//</debug>
fn = scope[fn] || me[fn];
}
event.addListener(fn, scope, options);
// If a new listener has been added (Event.addListener rejects duplicates of the same fn+scope)
// then increment the hasListeners counter
if (event.listeners.length !== prevListenerCount) {
hasListeners = me.hasListeners;
if (hasListeners.hasOwnProperty(ename)) {
// if we already have listeners at this level, just increment the count...
++hasListeners[ename];
} else {
// otherwise, start the count at 1 (which hides whatever is in our prototype
// chain)...
hasListeners[ename] = 1;
}
}
}