View Full Version : How to create a new store record with ID returned in AJAX response to POST?

3 Aug 2012, 7:45 AM
With ExtJS 4.0.7 I'm attempting to use a form to create a new record
that is then added to an existing store that communicates with some PHP
code that does a MySQL record insert and returns a success indicator and
the ID of the inserted record (no other record data is returned).
Because the initial ID is unknown a value of -1 is used, with the real
ID being created by MySQL as an auto index field. At this point
everything seems to be working as expected (records are being
successfully created), though on the client side the data store attached
to the grid view still has a record with an ID of -1. How do I get
ExtJS to use the returned ID?

Psudo code example of current process...

// create the application info data store
var StoreApplicationInfo = Ext.create('Ext.data.Store', {
autoDestroy: true,
autoLoad: true, //don't wait to load the data content
autoSync: true, //auto update store based on changes, use with allowSingle in proxy section
model: 'ModelApplicationInfo',
groupField: Global_AppInfoGroupingField,
sorters: { property: 'applicationDate', direction : 'DESC' },
proxy: new Ext.data.HttpProxy({
type: 'ajax',
url: 'AppName_Process_Data.php?data=info',
reader: {
type: 'json',
root: 'results.data',
totalProperty: 'recCount',
idProperty: 'applicationId',
successProperty: 'success'
}, //end reader
writer: {
writeAllFields: true,
allowSingle: true,
successProperty: 'success'
}, //end writer
exception: function(proxy, response, operation){
msg: operation.getError(),
icon: Ext.MessageBox.ERROR,
buttons: Ext.Msg.OK
} //end exception
}), //end proxy
listeners: {
//lots of console.info stuff, but no processing code in listeners
} //end listeners
}); //end object store

//save form changes menu function
function DoSaveInfoChanges() {
if(FormApplicationInfo.getForm().isDirty()) {
var FormRec = FormApplicationInfo.getForm().getValues(false, false, true, true);
if(FormRec.applicationId == '-1') {
//new rec stuff
//add causes required events to happen, but need to figure out how to get the store to account for the new ID that was previously -1
var inst = StoreApplicationInfo.add( FormRec );
//should synch be used here? add seems to do everything that is needed
} else {
//rec update stuff works fine
} //end new record check if
} else {
alert('Save Changes called -- nothing to do');
} //end is dirty check if
} //end function

//example of data returned from POST...
{ "results": {
"action": "insert",
"applicationId": "114",
"table": "info",
"success": true
} }

Am I going about this the wrong way? Does the data returned from the PHP code
doing the record insert need to be in a particular

Further edit: Sorry, this should have been placed in the Q&A section. Can someone move it?

3 Aug 2012, 2:29 PM
Do not set the ID. This will set record.phantom to false and it will be treated as an update. Let it default to the generated text value and the server is responsible to returning the new autoinc value.