Titanium JIRA Archive
Appcelerator Community (AC)

[AC-3252] Adding the same event listener twice should not be allowed

GitHub Issuen/a
TypeImprovement
Priorityn/a
StatusClosed
ResolutionNot Our Bug
Resolution Date2013-09-18T08:06:02.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsTitanium SDK & CLI
Labelsaddeventlistener, eventlistener, removeeventlistener
ReporterFokke Zandbergen
AssigneeMauro Parra-Miranda
Created2013-09-13T12:19:27.000+0000
Updated2016-03-08T07:57:39.000+0000

Description

As the following test shows: * Calling addEventListener on exact the same object, for the same event and using the same callback will cause the callback to get fired for as many times it has been added. * Calling removeEventListener on that same object, event and with the same callback will only remove one of the previous times it was added. I'd expect the same callback to be only added one, which should be doable by just checking if the object the callback param refers to is already a listener for that event.
var count = 0;
var fired = 0;

function listener() {
    fired++;
    firedLabel.text = 'Fired: ' + fired;
}

var win = Ti.UI.createWindow({
    backgroundColor:'#fff'
});

var remove = Ti.UI.createButton({
    top: 100,
    left: 25,
    title: 'Remove'
});

var countLabel = Ti.UI.createLabel({
    top: 100,
    text: 'Added: ' + count
});

var add = Ti.UI.createButton({
    top: 100,
    right: 25,
    title: 'Add'
});

var fire = Titanium.UI.createButton({
    top: 300,
    left: 25,
    title: 'Fire'
});

var firedLabel = Ti.UI.createLabel({
    top: 300,
    right: 25,
    text: 'Fired: ' + 0
});

add.addEventListener('click', function () {
    count++;
    countLabel.text = 'Added: ' + count;
    Ti.App.addEventListener('app:test', listener);
});

remove.addEventListener('click', function () {
    count--;
    countLabel.text = 'Added: ' + count;
    Ti.App.removeEventListener('app:test', listener);
});

fire.addEventListener('click', function () {
    fired = 0;
    firedLabel.text = 'Fired: ' + fired;
    Ti.App.fireEvent('app:test');
});

win.add(countLabel);
win.add(remove);
win.add(add);
win.add(firedLabel);
win.add(fire);

win.open();

Comments

  1. Fokke Zandbergen 2013-09-18

    Some info on why this was resolved as Not Our Bug would be appreciated.

JSON Source