Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store do something after sync with autoSync enabled

Tags:

extjs

extjs4.1

I'm trying to detect and remove those fields that after a sync are still on the store but are not added to the database (success: false). Then, return an error message to the user (with some error codes). However I can only see a "beforesync" event in store documentation.

Are there any possibility to do such a thing? I'm trying with "update" events but they are called after syncing only if the sync is successfull (otherwise they are called only before sync).

I can't really find an event that is fired after sync. Any solution for this?

Notice that I'm using autoSync, that's why I can't hook to the callback, otherwise everything will be easier.

Another important point I can see in the documentation is:

Code:

Ext.data.Model.EDIT
Ext.data.Model.REJECT
Ext.data.Model.COMMIT

Why REJECT event is never fired? I thought that if success = false REJECT would be called, do I require something like a 404 to obtain that result?

EDIT: No, I can't find a way to fire REJECT version of the update event. Any suggestion?

like image 586
Francesco Belladonna Avatar asked Dec 01 '22 23:12

Francesco Belladonna


2 Answers

This is, if you ask me, some design flaw in ExtJS.

AbstractStore has onCreateRecords, onUpdateRecords, and onDestroyRecords, which are empty functions you can override. You can call rejectChanges() if success is false there.

Ext.define('BS.store.Users', {
    extend: 'Ext.data.Store',    
    model: 'BS.model.User',

    autoSync: true,
    autoLoad: true,

    proxy: {
        type: 'direct',
        api: {
            create: Users.Create,
            read: Users.Get,
            update: Users.Update,
            destroy: Users.Delete,

        },
        reader: {
            type: 'json',
            root: 'data',
        },
    },

    onCreateRecords: function(records, operation, success) {
        console.log(records);
    },

    onUpdateRecords: function(records, operation, success) {
        console.log(records);
    },

    onDestroyRecords: function(records, operation, success) {
        console.log(records);
    },

});
like image 110
Izhaki Avatar answered Dec 04 '22 12:12

Izhaki


FYI: Sequence of events when updating a record (ExtJs 5.0.1)

Scenerio 1
javascript executes 
record.set('some data')  // Store has autoSync: true, The database will succeed

'update' event fires (operation = 'edit')
'beforesync' event fires
network traffic occurs database returns a success (json)
'update' event fires (operation = 'commit') 
'onUpdateRecord' method runs

Scenerio 2
javascript executes
record.set('some data')  // Store has autoSync: true, The database will fail

'update' event fires (operation = 'edit')
'beforesync' event fires
network traffic occurs database returns a failure (json)
'onUpdateRecord' method runs and calls rejectChanges()
'update' event fires (operation = 'reject') 
'exception' event fires

Two Notes: a) the last update and onUpdateRecord are reversed and b) if the onUpdateRecord does not call rejectChanges() the following 'update' event is not fired. This will leave the record in a dirty state which means that subsequent sync() will send it.

For record.add() the events are similar however I see that the 'add' event is not fired. The Ext documentation for the event says that it is fired but the same documentation does not say that the add() method will fire the event.

like image 32
dfox Avatar answered Dec 04 '22 10:12

dfox