PDA

View Full Version : Getting the full json response from Ext.data.JsonStore



broly
14 Apr 2011, 5:31 PM
Hi, I'm new at this forum.

I've really spend like three days trying to find the solution to this problem, please, take a look.
I'm working with Java, and from the server side I'm returning a json response in this way.



{
"status": {
"success": true
},
"rows": [
{
"price": "dato",
"change": "cabe",
"company": "dato1",
"pctChange": "ca"
},
{
"price": "otro dato",
"change": "un dato mas",
"company": "otra columna",
"pctChange": "aaa"
}
]
}


When the response contains status = true, then the "rows" data must be in the JsonStore structure using "rows" in the root statement. But when I receive status = false, then the response would be like this:




{
"status": {
"success": false
},
"rows": [
]
}


When the status = false, I need to show and message dialog to let the user know that it was an error during the request. So my question is, How do I do to parse the json before the json response is assigned to the JsonStore structure?

Thanks, hope you can help me

olivierpons
15 Apr 2011, 1:24 AM
Hi!
I don't know if this may help, but here's a part of my code that works (I'm like you: the user MUST know if something went wrong) .
I'm handling all the cases where something could go wrong in one place: globHandleAjaxError().
Why? Because if something has gone wrong it's absolutely not normal, so I can't continue, and the user can't continue (he/she has to refresh the page anyway). So all the cases where something went wrong have the same code => only one global function.


this.buttons = [{
text: 'Reload',
handler: function() {
this.getForm().load( {
url: '/myURLReload.php',
params: {
id_dossier: this.idDossier
},
waitTitle: 'Wait',
waitMsg: 'Reloading',
success:function(form, action) {
var b = Ext.util.JSON.decode(action.response.responseText);
if (typeof b.success=='undefined') {
globHandleAjaxError(response);
}
else if (b.success==false) {
globHandleAjaxError(response);
}
else {
console.log( 'Re-load ok !' );
}
},
failure:function(form, action) {
globHandleAjaxError(action.response,'Refresh error');
},
scope: this
} );
},
scope: this
},{
text: 'Sauver',
handler: function() {
this.getForm().submit({
url: '/wsSave.php',
params: {
write: 1,
id: this.idDossier
},
waitTitle: 'Wait',
waitMsg: 'Saving...',
success: function (form, action) {
var b = Ext.util.JSON.decode(action.response.responseText);
var b = Ext.util.JSON.decode(action.response.responseText);
if (typeof b.success=='undefined') {
globHandleAjaxError(response);
}
else if (b.success==false) {
globHandleAjaxError(response);
}
else {
if (b.msg) {
Ext.MessageBox.alert('Success', b.msg);
}
else {
Ext.MessageBox.alert('Success', 'Saved!');
}
}
},
failure:function(form, action) {
globHandleAjaxError(action.response,'Save error');
}
});
},
scope: this
}];

Screamy
15 Apr 2011, 4:32 AM
First off, your JSON structure is malformed. Ext expects 'success: true/false' to be at the 'root' of the data structure, like so:


{
"success": true,
"rows": [
{
"price": "dato",
"change": "cabe",
"company": "dato1",
"pctChange": "ca"
},
{
"price": "otro dato",
"change": "un dato mas",
"company": "otra columna",
"pctChange": "aaa"
}
]
}

Second, you can use the 'exception' listener on the JsonStore to catch an error should 'success: false' be returned in your JSON. Note that you have to tell the JsonStore the name of a property in the reponse which will hold the user message:


var store = new JsonStore({
autoLoad: true,
fields: ['price', 'change', 'company', 'pctChange'],
messageProperty: 'message',
root: 'rows',
url: ...,
listeners: {
exception: function(dataProxy, type, action, options, response, mixedArgs) {
Ext.Msg.alert('Error', response.message);
}
}
});



And for completeness, here's an 'error' JSON response:



{
"success": false,
"message": "We need more power, Captain! She'll never hold up under the strain!"
}

broly
15 Apr 2011, 7:47 AM
Thanks for your help.

I've tested the sample you gave me and it works perfectly jejeje.
So, what I didnīt know is that you have to put the "success" flag at the begining of the json structure.

Where can I find this specification details? because I've looked at

http://dev.sencha.com/deploy/dev/docs/

but nothing refers to the success property
:-(

Thanks

Screamy
15 Apr 2011, 11:17 AM
I've had trouble finding the spec as well. The closest thing I found was an example of JSON return data in the API docs for Ext.form.Action.Load (as used by Ext.form.FormPanel.getForm().load()).