Extjs 5.0 model POST values not received on server

2019-09-17 07:03发布

问题:

I am upgrading our Extjs 4.2 app to Extjs 5.0.
I am able to bring up all the read only pages but i am getting issues when i try to update/save the data. I will really appreciate your help!!

My model data values are not showing up on the server side, i am able to print model with console.log(model) and it has all the values , but on the server side it only has id and all the other parameters are showing as null.

Here is proxy in the model :


     Ext.define('MyApp.model.User', {
      extend: 'Ext.data.Model',
      id: 'user',
      proxy: {
        type: 'rest',
        url : '/rest/update/user',
        listeners: {
          exception: function(proxy, response, operation) {
            Ext.Msg.alert('Failed', 'Save the user Failed!');
          }
        }
      },
      fields: [
        {name: 'id', type: 'int'},
        {name: 'userName', type: 'string'},
        {name: 'country', type: 'string'}
        ]
    }

The controller :

onUserUpdateAction: function(button, event, action) { 

      var model = Ext.create('MyApp.model.User');    
      model.set('id', "123"); 
      model.set('userName', "john");
      model.set('country', "usa");
      ---
      model.commit() / without commit() it does not add the id in URL like /user/123
      model.save();
}

Here is the server side code :

@PUT
@Consumes({ "application/json" })
@Path("/Update/user/{id}")
updateUser(@PathParam("id") final int id, final User record);

First line log in the implementation class, id is there but all the other values are null

*** In updateUser() method, id : 123, record: User(id=123, **userName=null, country=null**)

回答1:

The problem here is that you try to fool the Ext. You create new record with id - normally ids are assigned by the server. Therefore, you need to commit it to clear it phantom (new record) flag, so that Ext thinks it is already existing record. But, after commit, the record has no modified fields and, by default, only modified fields are sent to the server. Therefore, you need a writer configured, something like this:

Ext.define('MyApp.model.User', {
    extend: 'Ext.data.Model',
    idProperty: 'id',
    fields: [
        {name: 'id', type: 'int'},
        {name: 'userName', type: 'string'},
        {name: 'country', type: 'string'}
    ],

    proxy: {
        type: 'rest',
        url : 'success.php',
        listeners: {
            exception: function(proxy, response, operation) {
                Ext.Msg.alert('Failed', 'Save the user Failed!');
            }
        }
        ,writer:{
             type:'json'
            ,writeAllFields:true
        }
    }
});