When sending Ajax or REST requests, a proxy of ExtJs 4 typically expects a response with the following parameters: data, success and message. The message parameter is optional, but it may come in handy when you want to show the request results to the user.
That’s how I do it:
function requestMessageProcessor(proxy, response) {
if (response && proxy) {
try {
var responseData = proxy.reader.getResponseData(response);
if (responseData.message) {
var messageDescription = 'Information'; // title of the alert box
var messageIcon = Ext.MessageBox.INFO;
if (!responseData.success)
{
var messageDescription = 'Error';
var messageIcon = Ext.MessageBox.ERROR;
}
Ext.MessageBox.show({
title: messageDescription,
msg: responseData.message,
buttons: Ext.MessageBox.OK,
icon: messageIcon
});
}
}
catch(err) {
// Malformed response most likely
console.log(err);
}
}
}
And here’s the part which should reside in proxy:
proxy: {
...
listeners: {
exception: function(proxy, response, options) {
requestMessageProcessor(proxy, response);
}
},
afterRequest: function(request, success) {
requestMessageProcessor(request.scope, request.operation.response);
}
}
It looks tricky because I didn't find a way to fetch the response message in afterRequest when the response is unsuccessful (success = false), so I had to add an exception listener.
Note that proxy itself may reside in either Model or Store. The code should work either way.
Also don't forget to add 'Ext.window.MessageBox' to Ext.require if you haven't.
If you know a better solution, or you know how this can be re-written, please comment!
I couldn’t find a better solution for this problem. Maybe this will me fixed in 4.10 but for now this fix work. I will share with you my approach :
Thanks! Although my solution works without an additional server parameter.
Amazing! Thank you for sharing this.
Thank you for posting this useful code.
On exception, how do I reject changes made on the store?
The grid using my store deleting row even though the store’s proxy fired exception.
So, using your code I can handle event and alerting user with a message but can not update UI (grid).
Thanks!
As far as I remember ExtJs doesn’t apply changes if server sends a response which says success = false. Sorry if this is irrelevant, I haven’t worked with ExtJs for a long time.
Pingback: Ext error window
Pingback: Ext error message