2

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.

Paul
  • 9,285
  • 6
  • 29
  • 37

2 Answers2

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.

sna19
  • 404
  • 5
  • 9
  • i am going with this method and simply deleting the id property from the jsonData: delete options.jsonData['post_id']. making persist false is inconvenient because i need the id for updates – Paul Apr 10 '12 at 18:28
1

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

Neil McGuigan
  • 46,580
  • 12
  • 123
  • 152