【问题标题】:EXTJS 6 - Store.Proxy.Sync - ExtraParams for Update but not CreateEXTJS 6 - Store.Proxy.Sync - ExtraParams 更新但不创建
【发布时间】:2017-03-30 13:58:34
【问题描述】:

我有一个商店,我已经向其中添加了记录,并在其中编辑了现有记录。

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

使用store.sync()

这会触发每个同步类型(C、R、U、D)的单独请求(使用代理 api 值)

对于每种同步类型,我需要传递一个动态的 extraParam(让我们简单地说,extraParam = {type: "Update"} 用于更新,extraParam = {type: "Add"} 用于添加),尽管在应用程序中这会更复杂,比如传递一个 JSON 对象基于正在同步的记录的用户详细信息或参数。

必须有办法做到这一点,而无需我手动编写同步功能。

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

【问题讨论】:

    标签: extjs synchronization store extjs6 extjs6-classic


    【解决方案1】:

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

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

    在捆绑不同的端点时,同步会进行字符串比较,并且由于所有端点定义不同,因此每个批次都是单独触发的。

    【讨论】:

    • 谢谢,是的,我在自己搜索解决方案时遇到了 api 配置,但这不是一个好的解决方案(尽管可能非常 EXT),因为覆盖代理的 api 列表会影响对代理的其他调用,它具有 ext 的异步性质,可能会产生奇怪的后果……但谢谢。
    • 这是一个完全按照您告诉我们您想要的解决方案,但祝您找到更好的解决方案...我想说您的问题不是不言自明的。
    【解决方案2】:

    这是我的最终解决方案

    我需要覆盖现有的同步功能,并且可以通过将新定义加载到覆盖文件夹中来实现,但我选择将其放在我的商店中。

    代码如下:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-29
      • 2015-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多