PDA

View Full Version : Form creates a new record, should change to received values once submitted?



KajaSheen
14 Mar 2012, 9:33 AM
Hey,

I have a form that submits via AJAX, in the return object more values are set as submitted, because for example a newly created record gets an ID assigned by the server. But the form does not reflect those changes of the record.

How do I get the updated record from the batch, after calling store.sync(), and feed the updated record into the form?

This seems a bit hacky for my taste?



store.sync({success: function(batch, options) {updateFormRecord(form, batch)}});

updateFormRecord = function(form, batch) {
if(batch.isComplete) {
for(var i in batch.operations) {
if(batch.operations[i].action == 'create') {
var record = batch.operations[i].records[0];
}
}
form.loadRecord(record);
}
}

dedoz
14 Mar 2012, 9:55 AM
Ext version you are using?
after calling store.sync() all the updated records will be set back to the store, you dont have to do anything.
your http request its correct ? did you verify is what you expect ?
your http response its correct ? did you verify is what you expect ?
and before sending the record to the form, did you verify that record had the expected data after the operation was finished ?

btw i guess form only edits 1 record from the store right ? why you sync the store when theres only one record thats begin edited ? so its clear to know which record you need to load to the form again.
its valid to edit many records and hold them in a store and then do a sync, but i guess you are only editing one record at time, because after the sync you are loading one record back to the form.

try something like


// to load an existent record to the form
record = myStore.getAt(34); // or Ext.create('myModel'); to creaty an new record.
form.loadRecord(record);

// in another handler
// to save those changes
values = form.getValues();
record = myStore.getAt(34); // or Ext.create('myModel') to save a new record.
record.set(values);
record.save({
callback:function(record)
{ console.log(record); // check the record and its data and verify everything is as you expected.
// do some stuff like loading the record back to the form
form.loadRecord(record);
}
})

remember to check http request and the response, and then with console.log(record) or record.getData() check if (after the response) has the correct data

(bad english)

KajaSheen
15 Mar 2012, 2:02 AM
Thanks. I am using 4.1 B3 right now.

I read that the record has a save function. But somehow I havn't made the connection to use it here. You are totally right. That makes it much easier.

But now I have another question ;)

If the operation is not successful I return the following:


{"success":false,"message":"Could not find entity"}

But the operation has not a response object. How do I access the error message in that case?

dedoz
21 Mar 2012, 6:33 PM
do this


someRecord.save({
callback: function(record,operation){

if (!operation.response.success)
{ console.log(operation.response.message);
... do other stuff ...
// you can mark a form field as invalid with the messga
field = this.basicForm.findField('someFormField');
field.markInvalid(operation.response.message);
}
else {
console.log("success!")
... do other stuff ...
}

},
scope:this
});