Using Typescript with JQuery I create a dialog and then attach click event to the two buttons and the "X" in the window title. The code actually creates a new SettingDlog object each time the user desires to open the dialog, however the bound click event seems to add an additional event for every subsequent time the user chooses to display the Settings dialog.
I thought perhaps the click event was being added to the DOM object, since that is independent of the typescript class (JavaScript object) but I can't see it in the DOM explorer so I don't think this is correct.
As a work around I explicitly remove the event handler when it is called but I don't understand why I have to do this since I'm creating a new SettingDlog object every time.
Code to show dialog:
let myDlog = new SettingsDlog();
myDlog.closedCallback = "settingsDlog_Closed";
myDlog.show();
Then in the show() method for the SettingsDlog I have:
show() {
$('#closeX').click({ isOK: false }, this.onClose.bind(this))
$('#btnCancel').click({ isOK: false }, this.onClose.bind(this))
$('#btnOK').click({ isOK: true }, this.onClose.bind(this));
}
Finally when the user clicks any of the three UIElements to close the dialog the onClose() method of the settings dialog is called:
onClose(event) {
this.dialogResult = event.data.isOK;
if (!AppHelper.isNullOrEmpty(this._closedCallback))
MainPage[this._closedCallback](this);
}
Finally the _closedCallback is called for the MainPage object:
settingsDlog_Closed(sender: SettingsDlog) {
if (!sender.dialogResult)
return;
// if here do something useful...
}
When I put a break point in settingsDlog_Closed and I click to show the Settings dialog a second time (or third...) it is acting as if a second (or third) click event is being added.
This is the code I'm adding to the onClosed method of the SettingsDlog as a workaround:
$('#closeX').off('click');
$('#btnCancel').off('click');
$('#btnOK').off('click');
Can someone help me to understand this odd behavior?