PDA

View Full Version : How to get server response after call model.save()



cwtuan
27 Jun 2013, 4:39 AM
Hi
I want to let user know why the request is failed after calling model.save().
So I let server responses {"success":true,"msg": "XXX"}.
But it seems that failure function is not able to get response.



record.save({
scope: this,
success: function(record, operation) {
console.log(operation.response); // I can get server response in success

},
failure: function(record, operation) {
console.log(operation.response); // undefined
}
});


I have seen someone listen on a exception event in proxy to get the response,
but it's not very graceful. Why does the failure can't get response?

slemmon
1 Jul 2013, 12:23 PM
You can use a callback config in save() like below:



Ext.define('MyModel', {
extend: 'Ext.data.Model',
fields: ['name'],
proxy: {
type: 'ajax',
url: 'data/json.json',
reader: 'json'
}
});


var model = Ext.create('MyModel');


model.save({
callback: function (records, operation, success) {
if (!success) {
console.log(operation.request.proxy.getReader().rawData.msg);
}
}
});


Example response:


{
"success": false,
"msg": "server response"
}


Or if you want to handle every server response globally you can do something like:


Ext.Ajax.on('requestcomplete', function (conn, response, options) {
var resp = Ext.decode(response.responseText);
if (resp.success === true) {
cosnole.log('success = true');
} else {
console.log(resp.msg);
}
});