PDA

View Full Version : Retrieve succesProperty after loading data to the store



nazol
11 Mar 2012, 12:59 AM
Hello! I don't understand how to figure out a status of the server response after loading data via http proxy. I need to load some records about movies from the database , so I've defined a model



Ext.define('Movie', {
extend:'Ext.data.Model',
fields:[
{name:'success',type:'int'},
{name:'title',type:'string'},
{name:'director',type:'string'},
{name:'released',type:'string'},
{name:'genre',type:'string'}
]
});


in the field success I assume, that there will be a return code from the server
After this I've created a store



var movies=Ext.create('Ext.data.Store', {
model:'Movie',
proxy:{
type:'ajax',
url:'movies.php',
reader:{
type:'json',
root:'rows',
successProperty:'success'
}
}
});


The php query to the database (I've omitted connection and sql text)



$query; // it contains right sql query
$result=mysql_query($query);
if(!$result){

Echo '{"success":"0"}';
}
else {
while($obj = mysql_fetch_object($result)) {
$arr[] = $obj;
}
Echo '{"success":"1",'.'rows:'.json_encode($arr).'}';
}

It generates the following output when succeeded (there is currently only one record in the database)



{"success":"1",rows:[{"title":"Office Space","director":"Mike Judge","released":"1999-02-19","genre":"Comedy"}]}


when I am trying to load data to the store


movies.load();

they are loading currectly except 'success' field - it is always 0, no matter was request successfull or not. And this is my general question - how to fix it? Please help

chramer
11 Mar 2012, 1:18 AM
you have this :

root:'rows'

It means that the model will look for data in the 'rows' branch of JSON..
But I really don't understand what are you trying to do.. If success is false,
there will be no rows in the grid, so just set a column 'success' with the
value '1' and it will be OK!

nazol
11 Mar 2012, 1:42 AM
Thank you for the response! I was trying to implement behaviour, mentioned here http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.reader.Json (http://docs.sencha.com/ext-js/4-0/#%21/api/Ext.data.reader.Json)



The server can return additional data in its response, such as the total number of records (http://docs.sencha.com/ext-js/4-0/#%21/api/Ext.data.reader.Json-cfg-totalProperty) and the success status of the response (http://docs.sencha.com/ext-js/4-0/#%21/api/Ext.data.reader.Json-cfg-successProperty). These are typically included in the JSON response like this:



{
"total": 100,
"success": true,
"users": [
{
"id": 1,
"name": "Ed Spencer",
"email": "ed@sencha.com"
}
]
}




If these properties are present in the JSON response they can be parsed out by the JsonReader and used by the Store that loaded it. We can set up the names of these properties by specifying a final pair of configuration options:



reader: {
type : 'json',
root : 'users',
totalProperty : 'total',
successProperty: 'success'
}



These final options are not necessary to make the Reader work, but can be useful when the server needs to report an error or if it needs to indicate that there is a lot of data available of which only a subset is currently being returned.

I just want to figure out how to retrieve successProperty from the reader

chramer
11 Mar 2012, 2:19 AM
store.load({
callback : function(records, operation, success) {
console.log(success);
}
});

nazol
11 Mar 2012, 2:43 AM
chramer (http://www.sencha.com/forum/member.php?240566-chramer),when I tried this, in FireBug console appeared 'true', it looks fine. But I've tried to change php script like this just because of curiosity



Echo '{"success":"0",'.'rows:'.json_encode($arr).'}';

and in console again appeared true. May be it is another 'success' field, or I am doing something wrong?

chramer
11 Mar 2012, 3:42 AM
oh yeah!
the response should be like this :

Echo '{"success":false}';
or this :

Echo '{"success":true,'.'rows:'.json_encode($arr).'}';

nazol
11 Mar 2012, 11:10 AM
Thank you, it worked! Thanks a lot for your help :)