Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
825
saveResult error-handling when using (non-transactional) restSettings
posted

I have an editable igGrid set up to save pending transactions using RESTful services. Excerpt:

    $("#ar_invoices").igGrid({
        aggregateTransactions : true,
        columns : [ blah blah ],
        features : [
        {
            name : 'Updating',
            editMode: "row",
            showDoneCancelButtons : true,
            columnSettings : [ blah blah ]
        } ],
        restSettings: {
            contentType: "application/x-www-form-urlencoded; charset=UTF-8",
            contentSerializer: iggridUrlEncode,
            create: {
                url: env_constants.DATASERVICES_URL + "/ar_invoices/"
            },
            update: {
                url: env_constants.DATASERVICES_URL + "/ar_invoices/"
            },
            remove: {
                url: env_constants.DATASERVICES_URL + "/ar_invoices/"
            }
        }
    });

The issue I'm currently wrestling with is error handling when a batch of changes are submitted. Here is my JavaScript function for saving pending changes:

function ar_invoice_changes_save() {
    $("#saveResult").text("");
    $("#ar_invoices").igGrid("saveChanges",
        function (data) {
            $("#user_prompt").text("Changes were saved successfully").fadeIn(3000).fadeOut(5000);
        },
        function (jqXHR, textStatus, errorThrown) {
            alert(jqXHR.responseText);
        });
}

My question is how to handle errors if the user adds multiple rows and saves them. The grid seems to keep all pendingTransactions around, which makes sense if they are saved in a transaction, but RESTful transactions are submitted independently. For example, consider the following scenario:

  1. User adds two rows.
  2. User clicks save, the first row is POSTed succesfully but the second returns an error.
  3. The user fixes the bad row, clicks Save again. All transactions are resubmitted, including ones that were already saved.
  4. The first row therefore gets POSTed twice, and (assuming there aren't any data constraints preventing it) a duplicate will be created.

This is not a problem for PUT, which is idempotent, and possibly not for DELETE, but POST-ing new rows is explicitly not idempotent.

At a high level, my options seem to be:

  1. Find a way to identify transactions that were successfully POSTed and avoid posting them again
  2. Bypass the grid's Add New Row operation in favor of my own form that forces the user to add one new row at a time
  3. Rewrite the POST operation to accept a list of new rows, then submit all new rows as a single POST from the grid somehow, as an atomic transaction

Does the grid have any support for #1? For example, knowing which pending transactions succeeded, if any? (Note: I am using "aggregateTransactions : true" so as I understand it, each edited row is effectively one transaction.)

Other suggestions also welcome. Thanks,

Parents
No Data
Reply
  • 825
    posted

    I thought of another workaround, possibly the most logical one, which would be to bypass the grid's saveChanges logic completely and do it myself. I would need to:

    i) iterate over pendingTransactions
    ii) serialize the correct HTTP transaction depending on whether the row was added, edited, or deleted
    iii) clear the pending transaction for each successful transaction

    The controller method OrderSaveData from the Basic Editing Sample (http://www.igniteui.com/grid/basic-editing) does some of this, except of course its written in ASP.NET, not JS. And I don't see any code for step iii, which would be needed prevent the problem I mentioned with duplicate POSTs.

    Please consider this approach along with my previously suggested approach. Again, I'm looking for suggestions how best to work around my issue, whether it is one of the ones  I came up with or some other approach. Thanks.

Children