无法使用Json-Server在我的Extjs应用程序中正确执行CRUD操作

时间:2015-04-08 15:35:28

标签: json extjs sencha-cmd

我正在使用Json-server来测试我的extjs网格中的CURD操作。我在json-server上放了一些虚拟数据,我能够读取,删除,更新和编辑这些数据。 但是当我使用我的extjs应用程序创建数据时,我无法删除或编辑该数据,因为自动生成的Id是“nameOfMyModel + autoIncrementNumber”。

我的商店是:

Ext.define('ThemeApp.store.peopleStore', {
    extend: 'Ext.data.Store',
        model: 'ThemeApp.model.peopleModel',
        storeId: 'peopleStore',
        pageSize: 500,  
        autoLoad: true,
        autoSync: true
});

模型是:

Ext.define('ThemeApp.model.peopleModel', {
    extend: 'Ext.data.Model',

    fields: [ 'id' ,'title','body'],

    proxy: {
        type: 'rest',
        //format: 'json',
        limitParam:"",
        filterParam: "",
        startParam:'',
        pageParam:'',

        url:'http://localhost:3000/posts',
        api: {
    read  : 'http://localhost:3000/db',
    create: 'http://localhost:3000/posts',
    update  : 'http://localhost:3000/posts' ,
    destroy : 'http://localhost:3000/posts' 
 }, 
        headers: {'Content-Type': "application/json" },        
        reader: {
        type: 'json',
        rootProperty:'posts'
        },
        writer: {
            type: 'json'
        }           
    }
});

我正在添加用户:

var UserStore = Ext.getStore('peopleStore');
var user = Ext.create('ThemeApp.model.peopleModel',{title: "Test", body: "Testing" });                      
user.save(); //POST /users
UserStore.load();

删除:

var grid =  button.up('gridpanel');
var selection = grid.getView().getSelectionModel().getSelection()[0];

if (selection) {

  UserStore.remove(selection);
  UserStore.load();                         
}

有谁能告诉我为什么我无法删除/更新我通过extjs app生成的记录? 简单帖子的ID就像 {1}}如1或2) 应用程序生成的记录是 http://localhost:3000/posts/(number

我可以在database.json中看到app生成的记录,但是浏览器说这个url不存在。

请指出我的错误

1 个答案:

答案 0 :(得分:1)

您无法删除它们的原因是因为它们未正确创建。首先要使用

生成顺序ID 模型中的

identifier: 'sequential',而不是生成数字ID 您可以在此处详细阅读Model identifier 执行此操作后,生成新对象并查看是否可以正确处理它们。