简体   繁体   English

代理从ExtJs存储发送乘法请求

[英]Proxy sends multiply requests from ExtJs store

My app send multiply request to server each time I insert new item to store. 每当我插入要存储的新项目时,我的应用就会向服务器发送乘法请求。 When the first item inserted it calls POST once, one more item ... twice and so on. 插入第一个项目时,它将调用一次POST,再调用一个项目……两次,依此类推。

Im new at ExtJs, so can you help me in solving some qustions: 我是ExtJs的新手,所以您可以帮助我解决一些问题:

  1. First of all what the sync method do, why (GET, POST, PUT, DELETE) methods are not called without store.sync() method invok? 首先, sync方法的作用是,为什么在没有store.sync()方法调用的情况下不调用(GET,POST,PUT,DELETE)方法?
  2. Why my post method multiply requests? 为什么我的post方法会倍增请求?
  3. Why store.remove() (DELETE) method throws 400 error ( Bad request ) when the PUT method works fine? 当PUT方法可以正常工作时,为什么store.remove()(DELETE)方法引发400错误( 错误请求 )?

Thank you in advance 先感谢您

my edit controller: 我的编辑控制器:

Ext.define('MVC.controller.Edit', {
extend: 'Ext.app.Controller',


init: function () {
    this.control({
        'editForm > button#SaveRecord': {
            click: this.onSaveButtonClick
        },
        'editForm > button#DeleteButton': {
            click: this.onDeleteButtonClick
        }
    });
},

onSaveButtonClick: function (btn) {
    //get reference to the form
    var detailView = btn.up('editForm');

    //get the form inputs
    var data = detailView.getValues();

    //see if the record exists
    var store = Ext.getStore('TestStore');
    console.log(data.id);
    var record = store.getById(data.id);

    if (!record) {
        record = Ext.create('MVC.model.Note', {
            title: data.title,
            created: new Date(),
            updated: new Date(),
            text: data.text
        });

        store.insert(0, record);
        store.sync();
        Ext.MessageBox.alert('Created', record.id);
        return;
    }

    record.set(data);

    store.sync();
    //manually update the record
    detailView.updateRecord();
},

onDeleteButtonClick: function (btn) {

    //get reference to the form
    var detailView = btn.up('editForm');

    //get the form inputs
    var data = detailView.getValues();

    var store = Ext.getStore('TestStore');
    var record = store.getById(data.id);
    store.remove(record);
    store.sync();
}
});

Store : 店铺:

Ext.define('MVC.store.TestStore', {
extend  : 'Ext.data.Store',

requires : [
    'MVC.model.Note'
],

storeId : 'TestStore',
model   : 'MVC.model.Note',
autoLoad: false,
proxy: {
    type : 'rest',
    url: 'rest/notes',
    actionMethods : {
        create  : 'POST',
        read    : 'GET',
        update  : 'PUT',
        destroy : 'DELETE'
    },
    reader: {
        type: 'json',
        rootProperty: 'data'
    },
    writer: {
        type: 'json'
    }
}
});

And model: 和型号:

Ext.define('MVC.model.Note', {
extend: 'Ext.data.Model',

fields: [
    {
        name: 'id',
        type: 'string'
    },
    {
        name: 'title',
        type: 'string'
    },
    {
        name: 'created',
        type: 'date'
    },
    {
        name: 'updated',
        type: 'date'
    },
    {
        name: 'text',
        type: 'string'
    }
]
});

Try to use record's methods: 尝试使用记录的方法:

1)Add proxy to you'r model like this: 1)将代理添加到您的模型中,如下所示:

Ext.define('MVC.model.Note',{
extend: 'Ext.data.Model',

fields: [
{
    name: 'id',
    type: 'string'
},
{
    name: 'title',
    type: 'string'
},
{
    name: 'created',
    type: 'date'
},
{
    name: 'updated',
    type: 'date'
},
{
    name: 'text',
    type: 'string'
}],

proxy:
{
    type: 'rest',
    url: 'rest/notes',
    reader:
    {
        type: 'json'
    }
}
});

2)Now you can use methods: record.save() - will send PUT if it was loaded to store from server and record have id, or POST if this record was created with record.create() ; 2)现在,您可以使用以下方法: record.save() -如果已将PUT从服务器加载到存储中并记录了ID,则将发送PUT;如果使用record.create()创建了该记录,则将发送POST record.destroy() - will send DELETE to server with id of record; record.destroy() -将DELETE发送给具有记录ID的服务器; You can use Ext.ModelManager.getModel('Model_name').load(id) to send GET request to server for single record. 您可以使用Ext.ModelManager.getModel('Model_name').load(id)GET请求发送到服务器以获取单个记录。 3)Use store.load() to GET array of records from server 3)使用store.load()从服务器获取记录数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM