PDA

View Full Version : [FIXED-622] Ext.util.Observable#hasListener() does not lower-case event name



YetAnotherFrank
23 Feb 2010, 12:54 AM
Ext version tested:

Ext 3.1.0
Ext Core 3.1.0
Ext 3.0.3


Adapter used:

ext


Description:

The method hasListener(eventName) in Ext.util.Observable is the only one that does not convert the eventName parameter to lower case. Thus, event listeners registered using an event name containing upper case letters are not found. This is inconsistent and very irritating, since all other methods like addListener(), removeListener(), fireEvent() convert the event name transparently.


Test Case:



<html>
<head>
<title>Bug: ext.util.Observable#hasListener()</title>

<script type="text/javascript" src="ext-core-debug.js"></script>
<!-- You can also use ext-all-debug.js, it contains the same relevant code. -->

<script type="text/javascript">
Ext.onReady(function() {
var o = new Ext.util.Observable();
o.addEvents("fooBar");
o.addListener("fooBar", function(x) {
alert("called: "+x);
});
o.fireEvent("fooBar", "A"); // invokes alert
o.fireEvent("foobar", "B"); // also invokes alert: case does not seem to matter!
alert("hasListener('foobar'): expected: true, actual: "+o.hasListener("foobar"));
// This is the bug:
alert("hasListener('fooBar'): expected: true, actual: "+o.hasListener("fooBar"));
// actual is false!
});
</script>
</head>

<body>
</body>
</html>



Steps to reproduce the problem:

Paste the example code into an HTML file in the directory where ext-core-debug.js is located and open it in any browser.


The result that was expected:

If for all other methods, case of the event name does not matter, we expect that this holds true for hasListener(), too. So o.hasListener("fooBar") is supposed to return true.


The result that occurs instead:

o.hasListener("fooBar") returns false, although we declared the event "fooBar" and we attached a listener for "fooBar" events.


Debugging already done:

Unlike all other Ext.util.Observable methods, hasListener() does not call toLowerCase() on the event name.


Possible fix:
Ext.util.Observable.js:


hasListener : function(eventName){
var e = this.events[eventName];
return ISOBJECT(e) && e.listeners.length > 0;
},


should become


hasListener : function(eventName){
var e = this.events[eventName.toLowerCase()];
return ISOBJECT(e) && e.listeners.length > 0;
},


If you think that an undefined, null, or non-String parameter should be tolerated (the documentation says its a String!), you could add code to convert eventName to a String.

evant
18 Mar 2010, 9:17 PM
Fixed in core rev 160.

YetAnotherFrank
18 Mar 2010, 11:17 PM
Thanks for your quick reaction. We appreciate this! It's always good to get rid of a patch.
Does "core" mean it is fixed in Ext JS and Ext Core?

Jamie Avins
19 Mar 2010, 8:21 AM
Yes, since ExtJS uses Ext Core it will also inherit this change.