Search code examples
extjsextjs4extjs4.1extjs4.2

ExtJS setTimeout call function but after destroy view I get some errors


I have a function that after 3000 seconds call other function to check new data for a grid.

onViewDestroy: function(view) {
        console.log('view destroy');
        var me = this;
        if(me.timer) clearTimeout(me.timer);
    },
    afterRender: function(){

     // start timer to load log grid in 3 secs
         me.logTimer = setTimeout(Ext.bind(me.checkProcessLog, me), 3000);

    },    
    checkProcessLog: function() {
                console.log('check process log');

                var me = this,
                   requestGrid = me.getRequestGrid(),
                   logGrid = me.getProcessLog(),
                   logStore = logGrid.getStore();

                var selectedRecord = requestGrid.getSelectionModel().getSelection()[0];

                // if the grid is expanded and the status of the process is pending or in progress
                if (!logGrid.collapsed && (selectedRecord.get('Status') == 0 || selectedRecord.get('Status') == 1)) {

                    logStore.load({
                        callback: function(records, op, success) {

                            if (success) {

                                // start timer to load again log grid in 3 secs
                                me.logTimer = setTimeout(Ext.bind(me.checkProcessLog, me), 3000);

                            }

                        }
                    });

                }

            },

The problem that I have is that if I close my view (destroy) when the function has been called then all my variables such:

  requestGrid = me.getRequestGrid(),
    logGrid = me.getProcessLog(),
    logStore = logGrid.getStore();

I get the error:

Cannot read property 'getStore' of undefined

And that make sense because the view has been destroyed. Any workaround to avoid this?


Solution

  • A simple check will do it :

    requestGrid = me.getRequestGrid(),
    logGrid = me.getProcessLog();
    if(!logGrid) return false
    logStore = logGrid.getStore();
    

    This also will stop the function beeing called.