1. #1
    Sencha Premium Member
    Join Date
    Oct 2011
    Location
    Huntsville
    Posts
    231
    Answers
    6
    Vote Rating
    -6
    mark0978 is infamous around these parts mark0978 is infamous around these parts

      0  

    Default Unanswered: How do you deal with records in the grid that fail to sync

    Unanswered: How do you deal with records in the grid that fail to sync


    Code:
    		store.add({name:alertName, active:true});
    		store.sync();
    All works well until the server response is a failure (either a 500, or a failure due to database constraints) then I have a record that is in the grid but not in the database.

    When "create" succeeds, the record from the server replaces the pre-save record in onCreateRecords, but when "create" fails, it throws an exception that you can catch in the proxy like so:

    Code:
            , listeners: {
                exception: function(me, response, operation, eOpts) {
                    var msg = (typeof(operation.error) == "string" ? operation.error : operation.error.statusText) || "Connection to server interrupted.";
                    Ext.MessageBox.alert("Exception", msg);
                }
            }
    What I can't seem to puzzle out is how do I get the unsaved record out of the grid in this case. I'd prefer the grid not get the record until it was saved, but I don't see how to do that. It has to be in the store to get into the sync call and putting it into the store makes it show up in the grid....

  2. #2
    Sencha Premium Member
    Join Date
    Oct 2011
    Location
    Huntsville
    Posts
    231
    Answers
    6
    Vote Rating
    -6
    mark0978 is infamous around these parts mark0978 is infamous around these parts

      0  

    Default Is this how it is done?

    Is this how it is done?


    So here is one method I've come up with, but it seems terribly convoluted, so I'm wondering if I'm just doing it wrong.
    Code:
    Ext.define('Trigger', {
        extend: 'Ext.data.Store'
        , model: 'TriggerModel'
        , storeId: 'triggerStore'
        , autoLoad: false
        , onRecordsSaved: function(operation) {
            if(operation.wasSuccessful()) {
                var records = operation.resultSet.records;
                for(var i=0; i < records.length; ++i) {
                    if(records[i].getId()) {
                        this.add(records[i]);
                    }
                }
            }
        }
        , addAfterSave: function(record) {
            var operation = Ext.create("Ext.data.Operation", {
                action: "create"
                , records: [record]});
            this.getProxy().doRequest(operation, this.onRecordsSaved,  this);
        }
    });
    and then I just call it with:

    Code:
         store.addAfterSave(store.createModel({name:alertName, active:true}));
    And all the magic just happens. One thing that makes me think something is wrong here is the call to proxy.commitRecords which basically ends up doing nothing.

    I still end up using the exception on the proxy object to display the error messages, but at least now I don't have orphans in the grid after a failure to save.