how do i prevent an extjs model/proxy from saving the empty primary id on create

我的未来我决定 提交于 2019-12-24 02:23:49

问题


i have defined my model with a REST proxy. it works fine for read (GET) and update (PUT) because those operations require a primary id. when i perform a create operation (POST) the proxy sends all the fields, including the empty primary id, to the server, which causes and error on the server. the server expects that no primary id is to be included for a create operation. how do i instruct extjs to not send the empty primary id value? ie. "{ 'model_id':'',...}"?

Ext.define('model', {
    extend : 'Ext.data.Model',
    idProperty : 'model_id',
    fields : ['model_id', 'first', 'last'],
    proxy : {
         type : 'rest'
    }
});

var mymodel = Ext.create('model',{last:'digler'});
mymodel.save() //posts "{ 'model_id':'', 'last':'digler'}"?

i want it to not include the primary id field at all on a create.


回答1:


I think that this is a wrong way to change the structure of request. In the case of usage REST, the responsibility to operate create, update requests are fully on server side.

In the case you still want to change parameters when create request is started, you can do it by "beforerequest" event:

Ext.Ajax.on("beforerequest", function( conn, options, eOpts){
  if (options.action=='create')
  {
    var newData = 
           {'name': options.jsonData.name, 'email': options.jsonData.email }; 
    options.jsonData = newData;
  }
});

Just for example I re-defines data fields hard coded, but, of course, it's possible to run through all fields in loop without writting its names in code.




回答2:


well, you have a pretty grumpy server then, don't you?

you can set it to null, but using

fields:[
{
name:'model_id',
type:'int'
useNull:true
}
]

though if model_id is a string it will still be ''

you can set persist = false on the field too, but then it won't save it on update

basically, tell your server admin to ignore the id if it's empty



来源:https://stackoverflow.com/questions/10084688/how-do-i-prevent-an-extjs-model-proxy-from-saving-the-empty-primary-id-on-create

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!