PDA

View Full Version : How to retrieve record id after adding new record -- the ID is issued at the server



yousefomar
27 Apr 2012, 1:29 AM
Hello,
it might be harder to explain the problem than the solution :)

I have created a store that uses proxy to communicate with a php server file, one of the operation is a POST method to add new record. the php file return 200 OK with { "success": true, "PatientID": '53' , "data": ''}

The question is how can read the returned PatientID ?

I tried to read it from "add" event but I was not successful..

here is the code I use:


// defining the store
Guard.PatientStore = Ext.extend(Ext.data.Store, {
constructor: function() {
Guard.PatientStore.superclass.constructor.apply(this, arguments);
},
proxy: new Ext.data.HttpProxy({url: 'proxies/Patients.php/patient'}), // <<----- proxy file
restful: true,
reader: new Ext.data.JsonReader({
idProperty: 'PatientID',
root: 'data'
}, Guard.PatientRecord), // <<----- patient record is defined in a seprate file
writer: new Ext.data.JsonWriter({
encode: true
})
});



var patientStore = new Guard.PatientStore(); // <<---- create a store object

patientStore.on('add', function(st, rec, index ){ // <<---- added an event listener for "add" event
x = rec[0].get('PatientID'); // <<--- this is the problem., PatientID I get is empty string , not as returned by php
});
....

// creating form
...
buttons: [{
text: ' Save ',
formBind: true,
handler: function(button) {
var newData = { // <<--- collect data from the form
PatientName: Ext.getCmp('PatientName').getValue(),
PatientNameEn: Ext.getCmp('PatientNameEn').getValue(),
Gender: Ext.getCmp('Gender').getValue(),
};
var newPatient = new patientStore.recordType(newData, undefined); //<<-- create a record
patientStore.add(newPatient); // <<-- add to the store. this triggers the call to php and php returns the 200 OK with PatientID


I appreciate your help
best regards

scottmartin
27 Apr 2012, 9:01 AM
If you place a breakpoint in your code on the event, what do you see for rec?

How about using
console.log(rec) to display the value in console?
do you a value for rec.data.PatientID?

Regards,
Scott.

yousefomar
27 Apr 2012, 2:58 PM
Hey Scott,
thanks for your quick reply,

console.log(rec) shows an Object with all fields set, accept PatientID is not listed under data, it only shows under fields

[Object { phantom=true, id="ext-record-8", data={...}, more...}]


and rec[0].data.PatientID show PatientID undefined

when I saw "phantom=true" (above) I added patientStore.commitChanges(); and a listener to "update" event and that had the same issue ..

any more ideas ?