简体   繁体   English

Extjs4 MVC将项目保存到服务器

[英]Extjs4 MVC save item to server

I am working with Extjs4.1 MVC. 我正在使用Extjs4.1 MVC。 What I am trying to do is save some data to the server but I do not know the proper format or how I should go about submitting the data to the server. 我想做的是将一些数据保存到服务器,但是我不知道正确的格式或如何将数据提交到服务器。 Here is what I am thinking but I do not believe the Ajax call should be in the controller, it should be in the model or in the store file? 这是我的想法,但我不认为Ajax调用应该在控制器中,应该在模型中还是在存储文件中?

method in my controller: 我的控制器中的方法:

    submit: function(value) { 
    data = {"id": 100, "tdt": "rTk", "val": "445"}   // test data
    Ext.Ajax.request({
        url: 'http://test.myloc.com/providerSvc/dbproxy.php',
        params: {
          'do':'insert',
          'object': 'stk',
          'values': data
        },
        success: function(response){
            alert('response.responseText);
        }
    })
   }

My store: 我的商店:

Ext.define('STK.store.Stack', {
extend: 'Ext.data.Store',
model: 'STK.model.Stack',
autoLoad: true,
proxy: {
    type: 'ajax',
    api: {
          read: 'http://test.myLoc.com/providerSvc/dbproxy.php?do=get&object=stack'
    },
    reader: {
        type: 'json',
        root: 'data',
        successProperty: 'success'
    },
    writer: {
        type: 'json'            
    }
}
});

my model: 我的模特:

Ext.define('STK.model.Stack', {
extend: 'Ext.data.Model',
fields: ['id', 'tdt', 'val']
});

store.sync() works only when the endpoints for GET and POST are same. store.sync()仅在GET和POST的端点相同时起作用。

What you can do is, for GET, set the extraParams by concatenating to the URL or by creating an object like 对于GET,您可以做的是,通过串联到URL或创建一个像这样的对象来设置extraParams

extraParams[urlKeys] = paramObject[urlKeys];
store.getProxy().setExtraParams(extraParams);

then, 然后,

Store.getProxy().setUrl(StoreUrlForGET);
Store.load({
callback : function(rec, operation, success) {
if (success) {}
else {}
});

and for POST write an AJAX request as, 并为POST编写一个AJAX请求,

Ext.Ajax.request({
url : StoreURLForPOST,
method : 'POST',
jsonData : Ext.JSON.encode(YourPostData),
success : function(response, request) {},
failure : function(response, request) {}
});

for this AJAX request you can, 对于此AJAX请求,您可以

Ext.Ajax.setDefaultHeaders({
"TokenId" : TokenValue
});

All of this code goes into your controller. 所有这些代码都进入您的控制器。

I think the store is the proper place to make the ajax call. 我认为商店是进行ajax调用的适当位置。

You can "save" one record by adding it to the store, and then calling the "sync()" function. 您可以通过将一条记录添加到存储中来“保存”一条记录,然后调用“ sync()”函数。

Something like this (beware: code not tested): 这样的事情(注意:未经测试的代码):

    var store = Ext.create("STK.store.Stack");


    var record =  Ext.create("STK.model.Stack");

    record.set(xvalues);
    record.isDirty = true;
    record.setDirty(true);  // I don't know if this line is required

    store.add(record);


    store.sync({
        success: function(batch, options){

          alert("OK!")                

        },
        failure: function(batch, options){ 
           alert("failure!")
        }
    });

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

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