View Full Version : [FIXED] Ext.util.Observable -> hasListener method is always returning true

25 Feb 2012, 6:26 PM
Ext.util.Observable hasListener is diferent when comparing to ExtJS 4.1.0 beta 2
Beta 2 Works

hasListener: function(ename) { var event = this.events[ename.toLowerCase()];
return event && event.isEvent === true && event.listeners.length > 0;

Beta 3 does not work

hasListener: function(ename) { return !!this.hasListeners[ename.toLowerCase()];

Ext version tested:

Ext 4.1.0 Beta 3
Browser versions tested against:

Any browser

Ext.util.Observable -> hasListener method is always return true
The result that was expected:

Checks to see if this object has any listeners for a specified event and return true ou false
The result that occurs instead:

always returning true
Test case:

Ext.onReady(function() {
var p = Ext.create('Ext.panel.Panel');



25 Feb 2012, 6:43 PM
Please post a test case:

Ext.onReady(function() {

var p = Ext.create('Ext.panel.Panel');


26 Feb 2012, 4:59 AM
Please post a test case:

Ext.onReady(function() {

var p = Ext.create('Ext.panel.Panel');


now is updated! is it suficient?

26 Feb 2012, 2:52 PM
No, I wasn't able to reproduce it. I ran your sample code in B3 in Chrome/FF/IE and it returned false in every case.

21 Apr 2012, 10:58 AM
this was not fixed yet

21 Apr 2012, 7:25 PM
As I said above I wasn't able to reproduce this, can you post a test case that demonstrates the issue?

22 Jul 2012, 11:36 AM
I am also seeing this. A simple test case is below, along with the values I get. As you can see, even after clearing the listeners, hasListener() still returns true, even though the listeners array is empty.

var hasListenerCheck1, hasListenerCheck2, hasListenerCheck3, listenerLengthCheck1, listenerLengthCheck2, testHandler, testObservable;

testHandler = function() {
return true;

testObservable = Ext.create('Ext.util.Observable');

hasListenerCheck1 = testObservable.hasListener('myTestEventName'); //false

testObservable.on('myTestEventName', testHandler(), this);
hasListenerCheck2 = testObservable.hasListener('myTestEventName'); //true
listenerLengthCheck1 = testObservable.events.mytesteventname.listeners.length; //1

hasListenerCheck3 = testObservable.hasListener('myTestEventName'); //true?!
listenerLengthCheck2 = testObservable.events.mytesteventname.listeners.length; //0

22 Jul 2012, 11:54 AM
I'll also add that if I create my own class and mix in Observable, calling the constructor on Observable and passing in 'this' ( this.mixins.observable.constructor.call(this) ) as the docs show, I get the same result.

22 Jul 2012, 2:31 PM
Thanks for the test case.

Deft JS
26 Jul 2012, 6:11 AM

I'm also running into the same issue demonstrated by Brian's test case with Ext JS 4.1.1.

This is breaking my unit tests, as I cannot accurately inspect an Observable to ensure the listeners have been cleaned up. Following a call to clearListeners(), hasListener() reports 'true' even though the internal listeners Array is empty.

26 Jul 2012, 6:39 AM
For now, I just wrote a function that digs into the object.events[ 'eventName' ].listeners.length value to check whether it is 0 or not.

So something like this (in CoffeeScript): object?.events?[ listenerName ]?.listeners.length

28 Jul 2012, 1:30 AM
The root of the problem is that clearListeners('foo') needs to do a "delete this.hasListeners.foo". In case that helps anyone.