PDA

View Full Version : Access response from the "save" operation on a Model instance?



pret
18 Oct 2011, 11:47 AM
Hi guys,

Having this:


record.save({
success: function(record, operation) {
// Here I need to access the response
},
failure: function(record, operation) {
// Here I need to access the response too
}
});

How can I access the response returned from the server? I couldn't find anything in the operation object.


Thanks!

gnube
20 Oct 2011, 7:00 AM
does this help?


e.record.save({
success: function(record, action){
this.commit(); //the commit cleans the grid
console.log( Ext.String.format("{0} record: {1}", 'Updated', record.getId()));
}
});


or perhaps a listener on your STORE work for you?
...

extend: 'Ext.data.Store',
listeners: {
write: function(store, operation){
var record = operation.getRecords()[0],
name = Ext.String.capitalize(operation.action),
verb;

if (name == 'Destroy') {
record = operation.records[0];
verb = 'Destroyed';
} else {
verb = name + 'd';
}

if (operation.action != 'create'){
console.log(Ext.String.format("{0} {1} user: {2}",name, verb, record.getId()));
}
}

pret
3 Nov 2011, 10:19 AM
Sorry for the delay, I came back today to this part of my app so now I can try your solution.

The problem is that I need to access the hole response. Suppose my response to a save action is:


{ success: true, msg: 'OK!!', data: {id: 1, nameOfEntity: 'EntityName'}, extraParams: {otherParam: 1234, andAnother: 'Another'}}

I'd like to access "extraParams" or any other parameter from the response. Is this possible?

BTW: One use case I have for this need is that I have to access errors thrown from the server because I couldn't find a way to make my forms be aware of the errors the server throws after POSTing or PUTting data for a record from the form. Which is the best way to do this?



Thanks in advance!

pret
3 Nov 2011, 10:29 AM
Nevermind, I've found it. I was using v4.0.2 and recently I've updated to v4.0.7. It seems that the operation argument now has a "response" attribute holding the response I was looking for:


record.save({
success: function(record, operation) {
operation.response // Here it is!
}});


Thanks a lot!

pret
3 Nov 2011, 10:31 AM
Oops, I didn't want to mark as the best answer that one. My PC is too laggy and when I clicked it did it on the wrong post.

Sorry about that. I don't know if there's a way to change this. If there is, I'll be happy to fix it.



Thanks!