Search code examples
javascriptajaxloadwait

Wait for ajax data load


I have a grid that pulls data from an ajax call and a video player. I'm trying to play the video after the data is loaded into the grid(DevExtreme.DataGrid). How do I do that?

var isLoaded = false;
        $.ajax({
            type: 'GET',
            url: '?handler=GetData&StartTime=' + eventStartTime + '&EndTime=' + eventEndTime,                
            success: function (response) {
                var obj = jQuery.parseJSON(response);
                var _dataGrid = $("#eventGridData").dxDataGrid("instance");
                _dataGrid.option('dataSource', obj);
                _dataGrid.reload();
                _dataGrid.refresh();
                isLoaded = true;
            },
            error: function (error) {
                alert("Error: " + error);
            }
        });
        //this needs to kick in after the data is loaded into the grid and I do a refresh(see above)
        if (isLoaded) {  
            //fetch matching video(by timestamp)
            playbackPlayer.connect({
                address: '127.0.0.1',
                port: '25200',
                timestart: timestart from the first record of the datagrid,
                timeend: timeend from the first record of the datagrid
            });
            playbackPlayer.play();
        }

Solution

  • I tried it this way using jQuery and it seems to be working:

    $.when(
    $.ajax({
                type: 'GET',
                url: '?handler=GetData&StartTime=' + eventStartTime + '&EndTime=' + eventEndTime,                
                success: function (response) {
                    var obj = jQuery.parseJSON(response);
                    var _dataGrid = $("#eventGridData").dxDataGrid("instance");
                    _dataGrid.option('dataSource', obj);
                    _dataGrid.reload();
                    _dataGrid.refresh();
                    
                },
                error: function (error) {
                    alert("Error: " + error);
                }
            })
    ).then.function(){ 
    
                //fetch matching video(by timestamp)
                playbackPlayer.connect({
                    address: '127.0.0.1',
                    port: '25200',
                    timestart: timestart from the first record of the datagrid,
                    timeend: timeend from the first record of the datagrid
                });
                playbackPlayer.play();
    });
    

    I still have to work on playing the video only if data is available in the grid. I think DevExtreme's datagrid has a method onLoadingChanged that would indicate that the data has been loaded.