[TIMOB-5042] removeEventListener() method does not remove event listener
| GitHub Issue | n/a |
|---|---|
| Type | Bug |
| Priority | Low |
| Status | Closed |
| Resolution | Invalid |
| Resolution Date | 2011-08-18T07:02:40.000+0000 |
| Affected Version/s | Release 1.7.1, Release 1.7.2 |
| Fix Version/s | n/a |
| Components | Android |
| Labels | n/a |
| Reporter | Paul Dowsett |
| Assignee | Don Thorp |
| Created | 2011-08-18T06:08:27.000+0000 |
| Updated | 2017-03-13T17:51:50.000+0000 |
Description
The following code demonstrates that removeEventListener() does not remove an event listener for an event attached to a window or
Ti.App:
var win = Ti.UI.createWindow({
backgroundColor: '#000',
exitOnClose:true
});
var button = Ti.UI.createButton({
title: 'Click to log events!',
height:80,
width: 300
});
win.add(button);
win.open();
button.addEventListener('click', function(){
Ti.API.info('Button click event fired');
win.fireEvent('testWinEvent');
Ti.App.fireEvent('testTiAppEvent');
});
win.addEventListener('testWinEvent', function(){
Ti.API.info('testWinEvent event fired');
});
Ti.App.addEventListener('testTiAppEvent', function(){
Ti.API.info('testTiAppEvent event fired');
});
win.removeEventListener('testWinEvent',function(){});
Ti.App.removeEventListener('testTiAppEvent',function(){});
The following is output to the console for all Titanium SDK versions tested:
I/TiAPI ( 598): (kroll$1: app://app.js) [177836,179502] Button click event fired
I/TiAPI ( 598): (kroll$1: app://app.js) [3,179505] testWinEvent event fired
I/TiAPI ( 598): (kroll$1: app://app.js) [0,179505] testTiAppEvent event fired
Note that no exception is generated.
I have realised this ticket is invalid, so am closing. As one event can have multiple event handlers, it is necessary to provide a reference to the function that you intend to remove. Hence, the code beneath works. I will raise a ticket to request improvement to docs.
var win = Ti.UI.createWindow({ backgroundColor: '#000', exitOnClose:true }); var button = Ti.UI.createButton({ title: 'Click to log events!', height:80, width: 300 }); win.add(button); win.open(); button.addEventListener('click', function(){ Ti.API.info('Button click event fired'); win.fireEvent('testWinEvent'); Ti.App.fireEvent('testTiAppEvent'); }); var testWinEventHandler = function(){ Ti.API.info('testWinEvent event fired'); }; var testTiAppEventHandler = function(){ Ti.API.info('testTiAppEvent event fired'); }; win.addEventListener('testWinEvent', testWinEventHandler); Ti.App.addEventListener('testTiAppEvent', testTiAppEventHandler); win.removeEventListener('testWinEvent', testWinEventHandler); Ti.App.removeEventListener('testTiAppEvent',testTiAppEventHandler);Closing ticket as invalid.