2013-05-14 53 views
1

我已經修改了sencha的作者example,但我無法更新我的數據庫中的數據。我有一個php腳本在服務器端執行此操作,但我無法通過extjs查看更新調用。我正在使用讀取器來獲取數據,並且它工作正常,但是當我嘗試更新發送到我的服務器的數據時,我從未接到過電話。用Ext Store Writer更新數據庫 - ExtJS 4.2

這裏是我的代碼:

Ext.require([ 
    'Ext.grid.*', 
    'Ext.data.*', 
    'Ext.util.*', 
    'Ext.state.*', 
    'Ext.form.*' 
]); 

Ext.onReady(function(){ 
    // Define our data model 
    Ext.define('Empresa', { 
     extend: 'Ext.data.Model', 
     fields: [ 
      { 
       name: 'Id', 
       type: 'int' 
      }, 
      { 
       name: 'Nombre', 
       type: 'string' 
      }, 
      { 
       name: 'Estado', 
       type: 'int' 
      }, 
      { 
       dateFormat: 'd/m/Y', 
       name: 'FechaCreacion', 
       type: 'string' 
      } 
     ] 
}); 


    // create the Data Store 
    var store = Ext.create('Ext.data.Store', { 
     // destroy the store if the grid is destroyed 
     autoDestroy: true, 
     model: 'Empresa', 
     //autoLoad: true, 
     proxy: { 
      type: 'jsonp', 
      api: { 
       read: 'http://fplweb2.localhost/empresa/listar/', 
       write: 'http://fplweb2.localhost/empresa/grabar/', 
       update: 'http://fplweb2.localhost/empresa/grabar/', 
       destroy: 'http://fplweb2.localhost/empresa/eliminar/', 
      }, 
      reader: { 
       type: 'json', 
       root: 'listaempresas' 
       }, 
       writer: { 
        type: 'json' 
       } 
     }, 
     sorters: [{ 
      property: 'start', 
      direction: 'ASC' 
     }] 
    }); 

    store.load(); 

    var rowEditing = Ext.create('Ext.grid.plugin.RowEditing', { 
     clicksToMoveEditor: 1, 
     autoCancel: false 
    }); 

    // create the grid and specify what field you want 
    // to use for the editor at each column. 
    var grid = Ext.create('Ext.grid.Panel', { 
     store: store, 
     columns: [ 
      { 
       xtype: 'gridcolumn', 
       dataIndex: 'Id', 
       text: 'Id', 
       editor: 'numberfield' 
      }, 
      { 
       xtype: 'gridcolumn', 
       dataIndex: 'Nombre', 
       text: 'Nombre', 
       editor: 'textfield' 
      }, 
      { 
       xtype: 'gridcolumn', 
       dataIndex: 'Estado', 
       text: 'Estado', 
       editor: 'numberfield' 
      }, 
      { 
       xtype: 'gridcolumn', 
       dataIndex: 'FechaCreacion', 
       text: 'FechaCreacion', 
       editor: 'datefield' 
      } 
     ], 
     renderTo: Ext.getBody(), 
     height: 600, 
     title: 'Lista de Empresas', 
     tbar: [{ 
      text: 'Agregar Empresa', 
      handler : function() { 
       rowEditing.cancelEdit(); 

       // Create a model instance 
       var r = Ext.create('Empresa', { 
        Id: store.getCount() + 1, 
        Nombre: 'Nueva Empresa', 
        Estado: '1', 
        FechaCreacion: Ext.Date.clearTime(new Date()) 
       }); 

       store.insert(0, r); 
       rowEditing.startEdit(0, 0); 
       console.log('Antes de' + store.getCount()); 
       //store.sync(); 
       console.log('Despues de: ' + store.getCount()); 
      } 
     }, { 
      itemId: 'removeEmpresa', 
      text: 'Eliminar Empresa', 
      handler: function() { 
       var sm = grid.getSelectionModel(); 
       rowEditing.cancelEdit(); 
       store.remove(sm.getSelection()); 
       if (store.getCount() > 0) { 
        sm.select(0); 
       } 
      }, 
      disabled: true 
     }], 
     plugins: [rowEditing], 
     listeners: { 
      'selectionchange': function(view, records) { 
       grid.down('#removeEmpresa').setDisabled(!records.length); 
      } 
     } 
    }); 
}); 

,這就是我的服務器端腳本返回讀者:

{"listaempresas":[{"Id":"1","Nombre":"DyS Nevados SRL","Estado":"1","FechaCreacion":"2013-05-13 10:40:00"}]} 

我曾在一些後讀,我需要做一個store.sync()但我得到一個錯誤。我正在使用extj 4.2。所以看documentation,那個方法不存在。我做錯了什麼?

回答

0

使您的商店的autoSync

var store = Ext.create('Ext.data.Store', { 
    autoSync : true 
    // ... 
} 

默認值是false。現在商店會在每次修改後自動同步。

+0

如果你想有條件地執行同步(如阻止同步,如果值沒有改變等),那麼或者將事件處理程序綁定到'編輯'事件 – dougajmcdonald 2013-05-14 18:22:47

+0

親愛的dougajmcdonald ...你能舉個例子嗎我在現實世界的應用程序中該如何做?我將不勝感激......謝謝! – sonseiya 2013-05-14 19:29:14

+0

親愛的Darin Kolev ...什麼是最好的方式來更新服務器中的數據...這是最好的方式?你能給我一個更好的例子嗎?謝謝... – sonseiya 2013-05-14 20:01:49