PDA

View Full Version : Store.add, store.sync, how do I update my record id?



redmagic
1 Aug 2012, 4:48 AM
Hi,

I've got a store with a rest proxy to communicate with the server.

My question is how do I update a added record with the id given to it by the server?

First, the record is added:

var newClass = Ext.create('SM.model.SchoolClass', {
name: name,
students: []
}); //Added without id


this.store.add(newClass);


this.store.sync();




The json going back and forth is as follows:

POST

{"id":null,"name":"testmij","students":[]}

Response

{"id":"b7ry8ina4ckr4k44x0ad5tpi6","name":"testmij","students":[]}

Then I handle the response on the stores write event:


this.store.on({
write: function (store, operation, options) {
this.handleResponse(operation);
},
scope:this
});
...
...
handleResponse: function (operation) {


if (operation.wasSuccessful()) {


switch (operation.action) {
case 'create':
var response = Ext.decode(operation.response.responseText);
this.addLocalClass(response.id, response.name, true);
this.getClassTabs().setActiveTab(response.id);
this.store.each(function(record) {
console.log(record.data.id,record.data.name)// This shows the added record, but without id
});
break;
case 'read':
break;
case 'update':
break;
case 'destroy':
this.removeLocalClass(operation.records[0].data.id);
break;
}


}
},


I would like to know what the proper way is to update the record with the id from the server.

Kind regards,

Arjen

scottmartin
1 Aug 2012, 8:33 AM
When you insert the record, it adds it to the store, the response then updates the record with id
When updating, the id determines the record that is updated.

This is handled for you as long as your response is correctly formatted.

Scott.

redmagic
6 Aug 2012, 12:34 AM
Hi,

Thanks for your reply.

I still have the problem that my local record is not automatically updated with the server response values.

A workaround I use now is to do it manually:

operation.getRecords()[0].data.id = response.id;

This does not feels very robust, and for batch operations it would require more workaround code.

Is there something wrong with the server response format? Or should I define the proxy on the model instead of the store?

Am a bit stuck here, help is very welcome.

Kind regards,

Arjen

scottmartin
6 Aug 2012, 8:59 AM
For one thing, you are not sending { success: true, data:[ .. ] }

Scott.

redmagic
29 Aug 2012, 12:05 AM
Thanks Scott,

I'll give it a try.