Search code examples
javascriptparse-platformdelete-row

parse.com delete too slow for re-render


I am adding a delete (destroy) object abilities to my web app. When i destroy the object and then re-render, my table still displays the destroyed line. when i delete again it fails to delete. I looks like my re-render is too fast for the destroy function. How can i re-render after the destroy is complete?

function fncBuildEditData(i) {
    var EditData = Parse.Object.extend("WorkOrders");
    var query = new Parse.Query(EditData);
    query.get(i, {
            success: function(myObj) {
                myObj.destroy({});
            },
            error: function(object, error) {
             alert("DELETION FAILED!");
        }
    });
    fncBuildWOTable();//rebuild table
}

Solution

  • Your query.get will return instantly, before it runs its success function, and likewise the myObj.destroy will also return before running it's success function. You should perform your reload inside the inner function, like so:

    function fncBuildEditData(i) {
        var EditData = Parse.Object.extend("WorkOrders");
        var query = new Parse.Query(EditData);
        query.get(i, {
                success: function(myObj) {
                    myObj.destroy(
                    {
                        success: function(x) { 
                            fncBuildWOTable();//rebuild table
                        },
                        error: function(x, error) {
                            alert("DELETION FAILED!");
                        }
                    });
                },
                error: function(object, error) {
                    alert("GET FAILED!");
                }
        });    
    }
    

    You have also mentioned that you are using the following code to bind the function:

    function ClickedRowButton() { 
        $(document).on('click', '.btnDeleteLine', function() {
            var row = $(this).parents('tr').attr('id'); 
            //var rowtext = $(this).closest('tr').text(); 
            //alert(row); 
            fncBuildEditData(row); 
        }); 
    }
    

    I believe that you were actually attempted to find only the closest element, rather than a collection of elements, so use this instead:

    var row = $(this).closest('tr').attr('id');