使用Ext Store Writer更新数据库 - ExtJS 4.2

时间:2013-05-14 17:38:13

标签: php database extjs writer

我修改了sencha的编写器example,但我无法更新数据库中的数据。我有一个PHP脚本在服务器端执行此操作,但我看不到extjs的更新调用。我正在使用读取器来获取数据,并且它工作正常,但是当我尝试更新发送到我的服务器的数据时,我从未接到电话。

这是我的代码:

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,该方法不存在。我做错了什么?

1 个答案:

答案 0 :(得分:0)

启用商店的autoSync

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

默认值为false。现在商店将在每次编辑后自动同步。

相关问题