Event handlers
Event handlers
I have a split button like this:
I need to attach a click event handler to each of those buttons like this
...
buttons: [
{ extend: "editPSCData", name: "editPSCVatDataButton",
split: [
{ extend: "editPSCData", name: "editPSCPreTaxDataButton",
text: lang === 'de' ? 'VSt.-Check §15 UStG' : 'Input tax check §15 UStG'},
{ extend: "editPSCData", name: "editPSCPreTaxCorrectionButton",
text: lang === 'de' ? 'Demnächst: VSt.-Korrektur §15a UStG' : 'Coming soon: Input tax adjustmt. §15a UStG'}
]
},
.....
table
.on ( 'select', function (e, dt, type, indexes) {
dt.buttons('editPSCVatDataButton:name').nodes().click(function() {
vatAssessmentType = 0; //"vat"
});
dt.buttons('editPSCPreTaxDataButton:name').nodes().click(function() {
vatAssessmentType = 1; //"input tax"
});
dt.buttons('editPSCPreTaxCorrectionButton:name').nodes().click(function() {
vatAssessmentType = 2; // "input tax update"
});
});
Then I wanted to turn off the event handler on "deselect":
table.
.on( 'deselect', function( e, dt, type, indexes ) {
//turn off click event handlers of the split buttons -- that turns off Editor as well!!
dt.buttons('editPSCVatDataButton:name, editPSCPreTaxDataButton:name, editPSCPreTaxCorrectionButton:name')
.nodes().off('click');
});
Then I noticed that this probably also turns off the Editor click event for the button because nothing happened any longer on click of the button after deselecting a row.
Two questions:
a) Is there an alternative so that I can just turn off the click events I set myself and nothing else?
b) Does it make sense to turn off my event handlers or is this all being taken care of sufficiently by the javascript garbage collector?
This question has an accepted answers - jump to answer
Answers
..
I managed to move my event handlers into the "init" event handler. That shouldn't create garbage that needs explicit cleaning. Had to make sure the split button is visible when attaching the handlers. That is working fine now
But I would still be interested in an answer to my two questions if anyone has an idea?!
For that sort of conditional event handler, sometimes I will use a flag on the element or some other variable, to indicate if the action should happen or not. Then you just add the event handler once and have an
if
condition inside the handler based on that flag.The other option is to use jQuery namespaces:
Allan