2011-11-29 18 views
3

在本文檔中,我已經找到實例化一個像這樣的店鋪:EXT JS Store的代理:讀者和作家

 
var store = Ext.create('Ext.data.Store', { 
    autoLoad: true, 
    model: "User", 
    proxy: { 
     type: 'ajax', 
     url : 'users.json', 
     reader: { 
      type: 'json', 
      root: 'users' 
     } 
    } 
}); 

代理有一個url配置。我對讀者特別感興趣。讀者指定數據交換格式(json)和根('用戶')。現在,換句話說,如果商店設置爲:autoLoad = true,那麼EXT JS將與指定的url建立一個Ajax連接,以便read。現在,我將如何爲上述同一商店配置一位作者?有人也告訴我這個:如果我配置一個作家,它會使用與代理中指定的相同的url嗎?我仍然對上面顯示的代碼中的作者和讀者感到困惑,您可以幫助我使用上述示例來顯示讀者和作者配置。謝謝。

回答

9

這裏是讀者,作家和API商店在我的應用程序的一個例子:

Ext.define('MyApp.store.Tasks', { 
    extend: 'Ext.data.Store', 
    model: 'MyApp.model.Task', 
    sorters : [{ 
     property: 'idx', 
     direction: 'ASC' 
    }], 
    autoSync:true, 
    proxy:{ 
     type: 'ajax', 
     reader: { 
      type: 'json', 
      root: 'data' 
     }, 
     writer: { 
      type: 'json', 
      writeAllFields : false, //just send changed fields 
      allowSingle :false  //always wrap in an array 
      // nameProperty: 'mapping' 
     }, 
     api: { 
       // read: 
       create: 'task/bulkCreate.json', 
       update: 'task/bulkUpdate.json' 
       // destroy: 
     } 
    }, 
    listeners : { 
     write: function(store, operation, opts){ 
      console.log('wrote!'); 
      //workaround to sync up store records with just completed operation 
      Ext.each(operation.records, function(record){ 
       if (record.dirty) { 
        record.commit(); 
       } 
      }); 
     }, 
     update:function(){ 
      console.log('tasks store updated'); 
     } 
    } 
}); 
+0

「寫」不觸發事件時自動同步=假。這是預期的行爲還是錯誤? –

+0

修正:因爲我正在使用會話,需要使用store.sync()來觸發事件 –

2

其實你是正確的 - 它會使用相同的URL作爲讀者。

代理服務器是客戶端的模型/存儲和另一端的服務器代碼之間的中介。 讀取器用於讀取數據,您可以配置格式化,指定根等等。作者負責保存/更新服務器的請求。

檢查這篇文章:http://edspencer.net/2011/02/proxies-extjs-4.html