PDA

View Full Version : data.Store accessing records



fernanpess
10 Sep 2009, 3:44 AM
Hi,

I'm using a data.Store that is fetched from a JsonReader. I'm sure the store is correctly filled - I have a Grid that shows it's data.
However I'm not being able to access it's records after the store loads. Sample bellow:

var store = getDashList("", ""); // the data.Store is returned

var rec = store.getAt(1);
alert(rec.get('notes')); // trying to access the field

The last line doesn't work, the page simply is not displayed.
Trying to debug I noticed that store.getCount() allways returns 0.

Any hints ? Thanks.

Philip

BitPoet
10 Sep 2009, 4:05 AM
Store.getCount() definitely works, so you're probably either not returning the expected store object from getDashList, or the store used in your grid is a different instance as the returned one. Though it's hard to tell without more code. Anyhow, doing 'console.dir( store )' and console.dir( grid.getStore() ) in firebug might be enlightening.

Condor
10 Sep 2009, 4:21 AM
Are you sure your store is actually loaded (and remember that loading is asynchronous!).

fernanpess
10 Sep 2009, 4:30 AM
Thanks BitPoet and Condor, however the store is correctly returned and it's the instance used in the grid. When testing I've noticed this sequence :
i) Grid header appears in the UI
ii) alert(store.getCount()) returns 0
iii) Grid is loaded with the 2 records in the store
It's as if the getCount() was being performed before the store is loaded, however it has already been returned from the getDashList function...
Perhaps Condor is right, having to do with the asynchronous behaviour.

The code is very simple:

function getDashList(startToken, sUser) {

var regDash = Ext.data.Record.create([
{name: 'id', type: 'int'},
{name: 'name', type: 'string'},
{name: 'notes', type: 'string'}
]);

var store = new Ext.data.Store({
url: 'http://localhost/srv/lib/...',
autoLoad: true,
reader: new Ext.data.JsonReader({
root: 'rows',
id: 'id',
totalRecords: '@total'
}, regDash),
sortInfo:{field:'name', direction:'ASC'}
});

return store;
}


Ext.onReady(function(){
...
var store = getDashList("", "");

var rec = store.getAt(1);
alert(rec.get('notes'));

// create the grid
var grid = new Ext.grid.GridPanel({
store: store,
columns: [
{header: "Id", width: 25, dataIndex: 'id', sortable: true, hideable: false},
{header: "Name", width: 100, dataIndex: 'name', sortable: false },
{header: "Notes", width: 100, dataIndex: 'notes', sortable: true },
],
renderTo:'example-grid',
width:350,
height:200,
border: false
});

alert(store.getCount());
});

Cheers,

Philip

fernanpess
10 Sep 2009, 5:01 AM
Thanks BitPoet and Condor, perhaps you're right and it has to do with the asynchronous nature of the load method...In fact what happens is the following:
i) Grid header is displayed in the viewport
ii) alert(store.getCount()) returns 0
iii) Grid UI is updated with the 2 records loaded

What I'm doing goes bellow:

function getDashList(startToken, sUser) {

var regDash = Ext.data.Record.create([
{name: 'id', type: 'int'},
{name: 'name', type: 'string'},
{name: 'notes', type: 'string'}
]);

var store = new Ext.data.Store({
url: 'http://localhost/srv/charts/dataProvider.aspx?elId=100&out=jsn',
autoLoad: true,
reader: new Ext.data.JsonReader({
// records will start w/ a "rows" tag
root: 'rows',
id: 'id',
totalRecords: '@total'
}, regDash),
sortInfo:{field:'name', direction:'ASC'}
});

return store;
}


Ext.onReady(function(){

var rec = store.getAt(1);
alert(rec.get('notes')); // if inserted this line, it halts execution

var grid = new Ext.grid.GridPanel({
store: store,
columns: [
{header: "Id", width: 25, dataIndex: 'id', sortable: true },
{header: "Nome", width: 100, dataIndex: 'name', sortable: false },
{header: "Notas", width: 100, dataIndex: 'notes', sortable: true },
],
renderTo:'example-grid',
width:350,
height:200,
title: 'Pipeline de Pagamentos2 - Entidades, Datas e Valores',
border: false
});


alert(store.getCount()); // allways returns 0
});

Thanks,

Philip

Condor
10 Sep 2009, 5:11 AM
Correct, try:

store.on('load', function(){
alert(store.getCount());
});

fernanpess
10 Sep 2009, 5:45 AM
Thanks Condor.

I've just inserted a listener for the load event, and it worked as expected.
Asynchronous behavior is the issue. It's seems like some reengineering has to be done...

Thank you both. Cheers,

Philip