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'
        }
    },
});
4

2 回答 2

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');
  }
});

考虑一下,如果商店的默认回调将被实施,这个解决方案可能会被认为是“黑客”。使用框架自己的解决方案将是一个更好的主意。

于 2020-05-13T12:37:10.993 回答
1

我会通过覆盖商店的同步方法来解决这个问题。

Sync 基本上是调用支持成功和失败回调的代理的批处理方法。因此,如果您在商店中设置了例如 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;
    }
});
于 2020-05-14T08:11:04.237 回答