Which Event/Option to use to remove event listeners?

Which Event/Option to use to remove event listeners?

stergiosstergios Posts: 12Questions: 2Answers: 0

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

  • kthorngrenkthorngren Posts: 21,174Questions: 26Answers: 4,923

    Interesting question. Maybe this example will help:
    http://live.datatables.net/cokupeci/1/edit

    Looks like the page event is the best option as it fires first and the current page rows are still in the table.

    Kevin

  • kthorngrenkthorngren Posts: 21,174Questions: 26Answers: 4,923
    edited January 2021

    Also you will want to consider searching and sorting as these functions could remove rows. from the DOM. The preXhr event might be better as it will be used by all three functions.

    Kevin

  • stergiosstergios Posts: 12Questions: 2Answers: 0

    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

  • stergiosstergios Posts: 12Questions: 2Answers: 0

    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:

    1. page existing data is still on the table,
    2. preDraw existing data is still on the table,
    3. drawCallback new data -- too late, old cells are already gone
    4. draw new data -- too late, old cells are already gone

    It appears I could use either the page or preDraw events to remove my listeners as they both still contain the olds cells. I choose page simply because it fires first, and I like the name!

    Thanks for the help,

    Stergios

This discussion has been closed.