PDA

View Full Version : While creating new record using REST Proxy id is not updated on Client side.



sarasa
9 Apr 2013, 3:38 AM
HI,
I am using rest proxy to perform CRUD Operations.
While am inserting data, i am getting proper response from the server with the id set.


{"success":"true","users":[{"email":"hr@abc.com","id":"11","name":"ABC"},{"email":"hr@abc.com","id":"12","name":"DDD"},{"email":"new@abc.com","id":"13","name":"new"}]}


But in client side, other email and name are updated, but id field is blank.
My Model code is


Ext.define('NSM.model.WPPolicyModel',{
extend: 'Ext.data.Model',
fields:[
{ name: 'id', mapping: "id" },
{ name: 'email', mapping: "email" },
{ name: 'name', mapping: "name" }
]
});


My Store code is:


Ext.define('NSM.store.WPPolicyStore', {
extend: 'Ext.data.Store',
model: 'NSM.model.WPPolicyModel',
autoLoad: true,
autoSync: true,
autoSave:true,

proxy: {
type: 'rest',
actionMethods: {
create: 'POST',
read: 'GET', // defaults to GET
update: 'PUT',
destroy: 'DELETE'
},
api: {
read: 'http://localhost:8080/commonui/rest/policies',
update: 'http://localhost:8080/commonui/rest/policies',
create: 'http://localhost:8080/commonui/rest/policies',
destroy:'http://localhost:8080/commonui/rest/policies'
},
headers: {
'Content-Type': 'application/json'

},


reader: {
type: 'json',
root: 'users',
idProperty: 'id',
successProperty: 'success'
},
writer: {
type: 'json',
writeAlFields:true

}


}


});


My Controller code on Save the of data is :


saveRule: function(button) { var win = button.up('window'),
form = win.down('form').getForm(),
values = form.getValues();
if(form.isValid()){
var record=form.getRecord(),
values=form.getValues();
if(record){
record.set(values);
this.getWpgridview().getStore().commitChanges();
}
else{


var record=Ext.create('NSM.model.WPPolicyModel');
record.set(values);
this.getWpgridview().getStore().insert(0,record );

this.getWpgridview().getStore().commitChanges();
}
}
else{
this.showAlertBox('Error','Not Valid',300,Ext.MessageBox.OK,Ext.MessageBox.Error);
}
this.getWpgridview().getStore().sync();
win.close();

},

Please help. Because id is not set, deleting the newly created item throws error.
Request your help

tvanzoelen
9 Apr 2013, 7:17 AM
first check if the idProperty is set to id and is not null, but it should be default set on id....

else you could try to listen to the sync callback and do a setId on each record that is returned.

sarasa
9 Apr 2013, 11:42 PM
Could you please post me an example as am newbie to this..

tvanzoelen
10 Apr 2013, 1:36 AM
try this,



record.set(values);
record.set('id', undefined);
this.getWpgridview().getStore().commitChanges();


it seems the id gets only updated if it is not already assigned.

sarasa
16 Apr 2013, 3:19 AM
Thanks.. It worked..