EXTJS 6-Store.Proxy.Sync-用于更新但不用于创建的ExtraParams

EXTJS 6-Store.Proxy.Sync-用于更新但不用于创建的ExtraParams,extjs,synchronization,store,extjs6,extjs6-classic,Extjs,Synchronization,Store,Extjs6,Extjs6 Classic,我有一个存储区,我在其中添加了记录并编辑了现有记录 现在我想将数据同步回服务器 使用store.sync 这将使用代理api值为每个同步类型C、R、U、D分别发出请求 对于每个同步类型,我需要传递一个动态的extraParam,让它变得简单,并说extraParam={type:Update}用于更新,extraParam={type:Add}用于添加,尽管在应用程序中这将是更复杂的事情,比如根据同步的记录传递用户详细信息或参数的JSON对象 一定有办法做到这一点,而无需我手动编写同步函数 如果

我有一个存储区,我在其中添加了记录并编辑了现有记录

现在我想将数据同步回服务器

使用store.sync

这将使用代理api值为每个同步类型C、R、U、D分别发出请求

对于每个同步类型,我需要传递一个动态的extraParam,让它变得简单,并说extraParam={type:Update}用于更新,extraParam={type:Add}用于添加,尽管在应用程序中这将是更复杂的事情,比如根据同步的记录传递用户详细信息或参数的JSON对象

一定有办法做到这一点,而无需我手动编写同步函数


如果可能的话,有人能举个例子吗?或者有更好的方法吗?

您的服务器代理使用包含各种URL的属性:

api: {
    read:'MyReadEndpoint',
    create:'MyCreateEndpoint',
    update:'MyUpdateEndpoint',
    delete:'MyDeleteEndpoint'
}
据我所知,您可以直接将GET参数添加到这些URL中:

api: {
    read:'MyEndpoint?action=read',
    create:'MyEndpoint?action=create',
    update:'MyEndpoint?action=update',
    delete:'MyEndpoint?action=delete'
}
捆绑不同端点时,同步会进行字符串比较,因为所有端点定义都不同,所以每个批都会单独启动。

这是我的最终解决方案

我需要超越现有的同步功能,可以通过将新定义加载到overrides文件夹来实现,但我选择将其放在我的存储中

其代码如下:

Ext.define('db_mubin.store', {
extend: 'Ext.data.Store'
,alias: 'store.db_mubin-store'

,require: 'db_mubin.model'
,model: 'db_mubin.model'

,proxy: {
    type:   'ajax'
    ,url:   '/api'
    ,reader: {
        type: 'json'
        ,rootProperty: 'data'
    }
    ,writer: {
        allowSingle: false  
    }
    ,extraParams: {
        calling: 'mubin'
    }
}

,listeners: {
    //add:  function(){this.sync({})},
    //update:   function(){this.sync({})},
    //remove:   function(){this.sync({})}
}

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

    options = options || {};
    options.params = options.params || {};

    //<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;
    me.isSyncing = true;

    if (toCreate.length > 0) {
        options.params.fetch =  'create';
        operations.create = toCreate;
        me.proxy.batch(Ext.apply(options, {
            operations: operations,
            listeners: listeners,
            params: options.params
        }));
        operations = {};
    }

    if (toUpdate.length > 0) {
        options.params.fetch = 'update';
        operations.update = toUpdate;
        me.proxy.batch(Ext.apply(options, {
            operations: operations,
            listeners: listeners,
            params: options.params
        }));
        operations = {};
    }

    if (toDestroy.length > 0) {
        options.params.fetch = 'destroy';
        operations.destroy = toDestroy;
        me.proxy.batch(Ext.apply(options, {
            operations: operations,
            listeners: listeners,
            params: options.params
        }));
        operations = {};
    }
    me.isSyncing = false;

    return me;
}
});

现在我可以随时调用sync,并传入额外的详细信息,例如能够提供API身份验证详细信息、用户详细信息、需要发送的任何内容,我都可以发送。

谢谢,是的,我在搜索解决方案时遇到了API配置,但这并不是一个好的解决方案,尽管可能非常简单,因为覆盖代理的api列表会影响到对代理的其他调用,这与EXT的异步性质有关,可能会产生奇怪的后果。。。但谢谢你。这是一个完全按照你告诉我们你想要的解决方案,但祝你好运找到一个更好的。。。我想说,你的问题不是不言自明的。