PDA

View Full Version : Store Sync Response



tomearly
24 Aug 2011, 4:00 AM
Hi,

I have a Ext.data.Store on which I doing a sync.

My sync works ok and I return some JSON. I can see the response in Firebug, but how do I use the response JSON?

I've looked at the examples, looked through the forum and the internet, but cannot see how to do this.

Below is the code I use for my store. If anyone is interested, i'm using the Bryntum Scheduler.


eventStore : Ext.create("Ext.data.Store", {
idProperty : 'Id',
model : 'Sch.model.Event2',
autoLoad: true,
proxy: {
type: 'ajax',
api: {
create: '/cgi-bin/EV_Rostering_Shifts_Save_New.pl', // Called when saving new records
read: '/cgi-bin/EV_Rostering_Events_Get.pl?branch=' + branch + '&view=' + view,
update: '/cgi-bin/EV_Rostering_Shifts_Save_Update.pl', // Called when updating existing records
destroy: 'myBackend.php?action=destroy' // Called when deleting existing records
},
reader: {
type: 'json',
model : 'Sch.model.Event2'
},
writer: new Ext.data.JsonWriter({
encode: true,
writeAllFields: true,
root: 'data'
}),
timeout: 10000
},
listeners: {
//add
add: function(store, records, index, options)
{
},
update: function(store, records, index, options)
{
}
//remove
//update
}
}),

ValterBorges
24 Aug 2011, 11:07 AM
Take a look at the store.write event for successfull update, for errors there is the proxy exception event.

tomearly
24 Aug 2011, 11:23 AM
Hi,

Thank you for the reply. Yes this is good for knowing if it was successful and is partly what I need.

When I have done the sync, I return some JSON from the script that processes the sync data.

I want to be able to use this data to

1) determine a success or failure ( and a reason )
2) to be able to return data to update the store ( this will contain IDs that are generated but the DB )

Any ideas how I can do this?

Thanks,

Tom

tomearly
25 Aug 2011, 5:23 AM
I've worked it out.

I've added an exception listener to the store and can then access the response upon syncing the store.

Something like this:-


eventStore : Ext.create("Ext.data.Store", {
idProperty : 'Id',
model : 'Sch.model.Event2',
autoLoad: true,
proxy: {
type: 'ajax',
api: {
create: '/cgi-bin/EV_Rostering_Shifts_Save_New.pl', // Called when saving new records
read: '/cgi-bin/EV_Rostering_Events_Get.pl?branch=' + branch + '&view=' + view,
update: '/cgi-bin/EV_Rostering_Shifts_Save_Update.pl', // Called when updating existing records
destroy: 'myBackend.php?action=destroy' // Called when deleting existing records
},
reader: {
type: 'json',
model : 'Sch.model.Event2'
},
writer: new Ext.data.JsonWriter({
encode: true,
writeAllFields: true,
root: 'data'
}),
timeout: 10000,
listeners: {
exception: function(proxy, res) {
//console.log(proxy);
//console.log(type);
var responseObj = Ext.JSON.decode(res.responseText);
var success = responseObj.success;

if(success == true)
{
EV.Rostering.Grid.eventStore.commitChanges();
EV.Rostering.Grid.eventStore.load();
}
else
{
alert('Unable to save shift(s)');
}
}
}
}
})