Check if DT instance was destroyed or not

Check if DT instance was destroyed or not

jLinuxjLinux Posts: 981Questions: 73Answers: 75
edited October 2015 in Free community support

I have a DT instance that is used to actively monitor sessions linked to the account you're logged in with. So if multiple ppl use it, you can watch the activity of the other accounts.

The main table that lists the sessions uses ajax sourced data, as well as the DataTable instances in the child rows. When the ( + ) is clicked to expand the child row, a table is drawn up via the format() function with a unique ID, then after format() is called to expand the child row and create the table, it will initialize a new DataTable instance on that table.

When the child table (which holds the activity for that session) is loaded, a function monitor_updates() is called via initComplete, which is handed the DT api instance. That will basically watch the AJAX data source for any updates, and if any are found, it will reload the table via ajax.reload().

=====================

Question: When the child row is collapsed, I need to destroy the DT instance and kill the loop within monitor_updates(), since the API if the DT instance is handed to monitor_updates(this), if I can just check if the table has been destroyed via that API, then everything should work.

The reason I need this, is because after the child-row has been collapsed, its currently still sending AJAX requests every N seconds, which it doesn't need to do anymore, since the table is closed.

Summary I have a loop that monitors the ajax data src for a table for any changes, and I need to be able to check if the table has been destroyed or not within that loop, (to know when to stop the loop)

The code snippet is HERE

Relevant Code:

  • Line 51 is where the child DT instance gets destroyed
  • Line 78 is where the monitor_updates(this) is fired to watch for updates on the child DT instance
  • Line 97 is where the monitor loop needs to check if the DT instance for the API in $dt is still active

Thanks for any help!

This question has an accepted answers - jump to answer

Answers

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    Heres a live.datatables instance, basically, when you click the Terminate DT button, the status should stop incrementing and say it was destroyed

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    I was able to figure it out, example here

    if( ! $.fn.DataTable.isDataTable( $dt.table().node() )){
        console.debug('QUITTING LOOP! No longer DT instance');
        clearInterval(updates_loop);
        return;
    }
    

    Thanks!!

  • allanallan Posts: 62,312Questions: 1Answers: 10,225 Site admin
    Answer ✓

    Another option that I would suggest considering is using the destroy() method to listen for the destroy event and kill of the interval there: http://live.datatables.net/culuxohi/2/edit .

    I'm concerned about the method you suggest in terms of memory leaks. If the table has been destroyed but an API instance is being clung onto so that the $dt.table().node() works, then it means that either a new table has been created, or the old table is lurking around after it has been destroyed...

    Allan

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    edited October 2015

    I'm concerned about the method you suggest in terms of memory leaks. If the table has been destroyed but an API instance is being clung onto so that the $dt.table().node() works, then it means that either a new table has been created, or the old table is lurking around after it has been destroyed

    I suppose I could see your worry, thanks for the alternative suggestion!

This discussion has been closed.