Which Event/Option to use to remove event listeners?
Which Event/Option to use to remove event listeners?
Description of problem:
I am using server-side processing and I use the drawCallback option to
attach my handlers for click events on a few cells every time pagination
occurs. The manual states "... the drawCallback callback is designed for
exactly that purpose and will execute on every draw."
I want to remove my event handlers, using jQuery's .off(), from the cells
before pagination removes the previous rows all-together in attempt to remove
dangling references and allow the javascript garage collector to succeed.
My question is which event or option should I use for this? Since the manual
explicitly states which option/event to use for attaching event handlers, i.e.
preDraw, I figure there must be an event/option meant for removing event
handlers as well.
Is preDraw the right one to use? Or perhaps preXhr.dt? Maybe the page.dt
event is the right one since it "will be fired before the table has been redrawn
with the updated data" and presumably the old rows are still on the table.
Of course I will experiment but I thought I would ask before doing so.
I'm working on a long running single page app, so I have to consider these
garbage collection issues.
Any guidance is greatly appreciated.
Thanks!
Stergios
Replies
Interesting question. Maybe this example will help:
http://live.datatables.net/cokupeci/1/edit
Looks like the
pageevent is the best option as it fires first and the current page rows are still in the table.Kevin
Also you will want to consider searching and sorting as these functions could remove rows. from the DOM. The
preXhrevent might be better as it will be used by all three functions.Kevin
Thanks Kevin, your example is a great way to see the event ordering. I have not decided which event to use yet, but when I do I'll report back here to have a complete record of the question.
Again, thanks!
Stergios
Here's my followup.
For removing my event listeners I choose to go with the 'page' event .
The order of events, and the data I observed are as follows:
pageexisting data is still on the table,preDrawexisting data is still on the table,drawCallbacknew data -- too late, old cells are already gonedrawnew data -- too late, old cells are already goneIt appears I could use either the
pageorpreDrawevents to remove my listeners as they both still contain the olds cells. I choosepagesimply because it fires first, and I like the name!Thanks for the help,
Stergios