如何在ExtJS中为存储添加同步回调?

问题描述 投票:0回答:1

我有一个商店,我想在每次进行某些操作时手动同步。

我看到 这个问题这个 也是。

我想知道的是,是否有办法在店铺配置上建立一个默认的同步回调函数。

我知道我可以做这样的事情。

store.sync({
        success: function(batch, options) {
            console.log('ok');
        },
        failure: function(batch, options) {
            console.log('not ok');
        }
    });

但我想在商店本身定义一个回调函数 然后我只需要调用store.sync();

下面是我正在使用的商店的一个例子。

Ext.define('MyApp.store.MyStore', {
    extend: 'Ext.data.Store',
    alias: 'store.MyStore',
    model: 'MyApp.model.MyModel',
    autoLoad: true,
    proxy: {
        type: 'rest',
        url: '../../api/myEndPoint',
        noCache: false,
        reader: {
            type: 'json',
            rootProperty: 'data',
            successProperty: 'success',
            totalProperty: 'total'
        },
        actionMethods: {
            read: 'GET',
            destroy: 'DELETE'
        },
        extraParams: {
            type: 'aux'
        }
    },
});
javascript extjs extjs6-classic extjs6.2
1个回答
1
投票

如果有办法通过框架的手段来实现你想要的东西,我找不到。

总有另一种选择,你可以把同步包在一个函数里,每次你想用这些特定的回调来同步时,就调用这个函数。

function sync({
    success = (batch, options) => console.log('ok'),
    failure = (batch, options) => console.log('not ok')
} = {}) {
    store.sync({success, failure});
}

你可以通过传递参数来改变参数中的默认函数。

// default paramters will be used
sync();

// just one parameter
sync({
  failure: () => {
    console.log('new failure handler');
  }
});

// both parameters
sync({
  success: () => {
    console.log('new success handler');
  },
  failure: () => {
    console.log('new failure handler');
  }
});

考虑到如果商店的默认回调将被实现,这个解决方案很可能会被认为是一个 "黑客"。走框架自己的解决方案会是一个更好的主意。


0
投票

我会通过重写store的 "回调 "来解决这个问题。同步 方法。

同步基本上是调用代理的 批次 方法,支持成功和失败回调。因此,如果您在您的商店中设置了syncSuccess和syncFailure回调,那么只有在同步商店后,它们才会被调用。

注释。

  • 我在7.1.0版本上测试了这个解决方案。请检查您的版本的同步方法是否存在差异。
  • 当回调没有在存储中定义时,你需要添加额外的逻辑。
Ext.define('Overrides.data.Store', {
    override: 'Ext.data.Store',

    sync: function(options) {
        var me = this,
            operations = {},
            toCreate = me.getNewRecords(),
            toUpdate = me.getUpdatedRecords(),
            toDestroy = me.getRemovedRecords(),
            needsSync = false;

        //<debug>
        if (me.isSyncing) {
            Ext.log.warn('Sync called while a sync operation is in progress. ' +
                         'Consider configuring autoSync as false.');
        }
        //</debug>

        me.needsSync = false;

        if (toCreate.length > 0) {
            operations.create = toCreate;
            needsSync = true;
        }

        if (toUpdate.length > 0) {
            operations.update = toUpdate;
            needsSync = true;
        }

        if (toDestroy.length > 0) {
            operations.destroy = toDestroy;
            needsSync = true;
        }

        if (needsSync && me.fireEvent('beforesync', operations) !== false) {
            me.isSyncing = true;

            options = options || {};

            me.proxy.batch(Ext.apply(options, {
                operations: operations,
                listeners: me.getBatchListeners(),
                $destroyOwner: me,
                success: me.syncSuccess,
                failure: me.syncFailure
            }));
        }

        return me;
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.