2016-11-16 33 views
-3

我有我添加記錄商店,並在EXTJS 6 - Store.Proxy.Sync - ExtraParams的更新而不是創建

編輯現有記錄,現在我想將數據同步回服務器。 。

使用store.sync()

這觸發關閉單獨的請求對每個同步類型(C,R,U,d)(使用代理API值)

對於每個同步型的,我需要通過一個動態的extraParam(讓它變得簡單,並說extraParam = {type: "Update"}爲更新,extraParam = {type: "Add"}爲增加),儘管在應用中這將會更復雜一些,比如根據正在同步的記錄傳遞用戶詳細信息或參數的JSON對象。

必須有辦法做到這一點,而不必手動編寫出同步功能。

有人可以舉一個例子,如果這是可能的或更好的方法?

回答

0

你的服務器代理採用包含各種URL的api屬性:

api: { 
    read:'MyReadEndpoint', 
    create:'MyCreateEndpoint', 
    update:'MyUpdateEndpoint', 
    delete:'MyDeleteEndpoint' 
} 

據我所知,你可以直接添加GET參數到以下網址:

api: { 
    read:'MyEndpoint?action=read', 
    create:'MyEndpoint?action=create', 
    update:'MyEndpoint?action=update', 
    delete:'MyEndpoint?action=delete' 
} 

當捆綁不同的端點,同步執行字符串比較,並且由於所有端點定義都不相同,因此每個批次都單獨觸發。

+0

謝謝是的,我在搜索解決方案的時候遇到了api配置,但這不是一個好的解決方案(儘管可能非常EXT),因爲覆蓋代理的api列表會影響代理的其他調用,的分機,可能會有奇怪的後果......但謝謝你。 – TolMera

+1

這是一個解決方案,它完全符合你告訴我們你想要的,但祝你好運找到更好的解決方案......我想說你的問題不是自明的。 – Alexander

0

這是我的最終解決方案

我需要在乘坐現有的同步功能,並可以通過加載新定義成覆蓋文件夾這樣做,而是選擇了把這個在我的商店。

操作的代碼如下:

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認證的詳細信息,用戶信息,什麼,我需要發送,我可以發送。

相關問題