PDA

View Full Version : Restful Store Example - totalProperty/No Data



sportda
13 Oct 2010, 6:06 AM
I'm having trouble getting my first ExtJS grid based on the restful store example to display data. I notice the the data from the example as viewed in firebug has no totalProperty yet the data displays properly. From everything I've read a properly set totalProperty is crucial and it's not hard to understand why. What is difficult to understand is why there is no totalProperty in the example data and how it works without it.

Also, when I execute the example (with no changes) locally on my computer I get this in firebug...
<br /> <b>Notice</b>: Trying to get property of non-object in <b>C:\wamp\www\ext-3.2.1\examples\restful safe\
remote\lib\request.php</b> on line <b>47</b><br /> {"success":true,"message":"Loaded data","data":[{"id":1,"first":"Fred","last":"Flintstone","email":"fred
@flintstone.com"},{"id":2,"first":"Wilma","last":"Flintstone","email":"[email protected]"},{"id":3,
"first":"Pebbles","last":"Flintstone","email":"[email protected]"},{"id":4,"first":"Barney",
"last":"Rubble","email":"[email protected]"},{"id":5,"first":"Betty","last":"Rubble","email":"[email protected]"},
{"id":6,"first":"BamBam","last":"Rubble","email":"[email protected]"}]}

In my code I get a related error here...


$params = json_decode(stripslashes($raw),true);
$r = print_r($params,true);
logToFile("c:/restful_log.txt", "Params_r: $this->params");
$this->params = $params->data; <<<<< Error here


Not knowing what else to do I changed it to...
$this->params = $params;

...and the error goes away, and I get data that looks reasonable in firebug, except that there's no totalProperty...
{"success":true,"message":"Loaded data","data":"[{"id":"5","name":"Boatey","owner1_no":"28"},
{"id":"9","name":"Joey","owner1_no":"30"},{"id":"11","name":"April","owner1_no":"33"},
{"id":"12","name":"Cooper","owner1_no":"33"}]"}

There are quotes around my ID data, not sure if that's a problem.

..and I get no data in my grid.

Can anyone help me out here? I've been spinning my wheels for two days on this and although
I'm learning what firebug can do for me I feel like I'm making any progress on getting this to work.

Here's my code....



var App = new Ext.App({});

var Thorses_grid = Ext.extend(Ext.grid.GridPanel,
{
title: 'Horses',
frame: false,
height: 300,
width: 500,
viewConfig:
{
forceFit: true
},
editor: new Ext.ux.grid.RowEditor
({
saveText: 'Update'
}),


onAdd: function(btn, ev)
{
var u = new this.store.recordType({
name: '',
owner1_no: ''
});
this.editor.stopEditing();
this.store.insert(0, u);
this.editor.startEditing(0);
},


onDelete: function()
{
var rec = this.getSelectionModel().getSelected();
if (rec)
{
this.store.remove(rec);
}
},

initComponent: function()
{
var proxy = new Ext.data.HttpProxy
({
url: 'app.php/horses'
});

var reader = new Ext.data.JsonReader({
totalProperty: 'total',
successProperty: 'success',
idProperty: 'id',
root: 'data',
messageProperty: 'message' // attribute in server response for user message...
},[
{ name: 'id' },
{ name: 'name', allowBlank: false},
{ name: 'owner1_no', allowBlank: false}
]);

var writer = new Ext.data.JsonWriter({
encode: false
});

var store = new Ext.data.Store({
id: 'horse',
restful: true,
proxy: proxy,
reader: reader,
writer: writer

});

var config =
{
store: store,
plugins: [this.editor],
columns:
[{
header: "ID", width: 40, sortable: true, dataIndex: 'id'},
{
header: "Name",
width: 100,
sortable: true,
dataIndex: 'name',
editor: new Ext.form.TextField({})
},{
header: "Owner",
width: 50,
sortable: true,
dataIndex: 'owner1_no',
editor: new Ext.form.TextField({})
}],
tbar:
[{
text: 'Add',
iconCls: 'silk-add',
handler: this.onAdd,
scope: this
}, '-', {
text: 'Delete',
iconCls: 'silk-delete',
handler: this.onDelete,
scope: this
}, '-']
};
Ext.apply(this, Ext.apply(this.initialConfig, config));
Thorses_grid.superclass.initComponent.apply(this, arguments);
},
onRender: function()
{
this.store.load({params:{start:0, limit:10},

callback: function(){

Ext.Msg.show({
title: 'Store Load Callback',
msg: 'store was loaded, data available for processing',
modal: false,
icon: Ext.Msg.INFO,
buttons: Ext.Msg.OK
});

}
});


Thorses_grid.superclass.onRender.apply(this, arguments);
}
});
Ext.reg('horses_grid', Thorses_grid);

Condor
13 Oct 2010, 6:10 AM
totalProperty is only needed when using paging (otherwise getTotalCount() is always equals to getCount(), so it doesn't need to be specfied).

Your JSON data is invalid. Your 'data' root is a String and not an Array!

sportda
13 Oct 2010, 6:21 AM
I see the quotes, I wonder where those came from! Thanks!!

sportda
13 Oct 2010, 6:54 AM
I'm still learning Javascript, can you please help me figure out *why* it's a String?


public function view()
{
$res = new Response();
$res->success = true;
$res->message = "Loaded data";
$res->data = Horse::all();
$rd = print_r($res->data,true);

logToFile("c:/restful_log.txt", "Horses::view: $rd");
logToFile("c:/restful_log.txt", "Total: $res->total");

$x = $res->to_json();
logToFile("c:/restful_log.txt", "Horsesx::view: $x");

return $res->to_json();
}


In my logs the quotes are not there until after to_json is called...
Horses::view: [{"id":"5","name":"Boatey","owner1_no":"28"},{"id":"9","name":"Joey","owner1_no":"30"},{"id":"11","name":"April","owner1_no":"33"},{"id":"12","name":"Cooper","owner1_no":"33"}]
Total: 1
Horsesx::view: {"success":true,"message":"Loaded data","data":"[{"id":"5","name":"Boatey","owner1_no":"28"},{"id":"9","name":"Joey","owner1_no":"30"},{"id":"11","name":"April","owner1_no":"33"},{"id":"12","name":"Cooper","owner1_no":"33"}]"}


public function to_json()
{
return stripslashes(json_encode(
array(
'success' => $this->success,
'message' => $this->message,
'data' => $this->data
))
);
}
}

I added the stripslashes to to_json, it seemed like a good idea.

Condor
13 Oct 2010, 7:02 AM
1. Horse::all() shouldn't json_encode the data.
2. Why is your to_json function calling a stripslashes function?