PDA

View Full Version : store removeAll() and add() not working as expected



raj_plays
23 Feb 2011, 11:33 PM
Hi,
My PivotGrid is linked to a store.
In the load event of the store, I do a removeAll() and then add an array of Record objects to it.
The problem is that removeAll is not removing the old records.
The store, after load, has 3 records and all the alerts with st.getTotalCount() show 3.
It seems removeAll() and add() is not working. The new_records array has 24 items after the for loops have run.
Please help.


var statistics = Ext.data.Record.create([
{name: 'time_range', type: 'string'},
{name: 'day', type: 'string'},
{name: 'duration', type: 'int'}
]);

statisticsDataStore = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({
url: 'CampaignService.asmx/getDurations',
method: 'POST'
}),
reader: new Ext.data.XmlReader({
record: 'Table'
}, statistics),
listeners: {
load: function (st, records, options) {
var new_records = new Array();
for (var i = 0; i < records.length; i++) {
var hour_start = records[i].get('time_range').substr(0, 2);
var hour_end = records[i].get('time_range').substr(6, 2);
var day = records[i].get('day');
var duration = records[i].get('duration');

for (var j = hour_start; j <= hour_end; j++) {
new_records.push(new statistics({
time_range: j,
day: day,
duration: duration
}));
}
}

alert(st.getTotalCount())
st.removeAll();
alert(st.getTotalCount())
st.add(new_records);
alert(st.getTotalCount())
}
}
});

Condor
23 Feb 2011, 11:53 PM
This would load the store a second time while the store is still loading for the first time.

I would recommend solving this in the readRecords method, e.g.


var statistics = Ext.data.Record.create([{
name: 'time_range',
type: 'string'
}, {
name: 'day',
type: 'string'
}, {
name: 'duration',
type: 'int'
}]);

var reader = new Ext.data.XmlReader({
record: 'Table'
fields: statistics
});
reader.readRecords = function (doc) {
var data = Ext.data.XmlReader.prototype.readRecords.call(this, doc),
records = [];
if (data.success) {
for (var i = 0; i < data.records.length; i++) {
var records = data.records[i],
hour_start = record.get('time_range').substr(0, 2),
hour_end = record.get('time_range').substr(6, 2),
day = record.get('day'),
duration = record.get('duration');

for (var j = hour_start; j <= hour_end; j++) {
records.push(new statistics({
time_range: j,
day: day,
duration: duration
}));
}
};
}
return {
records: records,
success: data.success,
totalRecords: records.length
};
});

statisticsDataStore = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({
url: 'CampaignService.asmx/getDurations',
method: 'POST'
}),
reader: reader
});

raj_plays
24 Feb 2011, 3:41 AM
Thank you, Condor.
Your code works beautifully.
Here you are overrode the readRecords method of Reader.
Does this mean that the underlying records in the store remain unchanged but when the records are fetched, they are "formatted" in the way we want?
When is the readRecords method called anyway?

Also, what was wrong with my idea of calling removeAll() and then doing an add() on the load event of the store?
The function declared on 'load' is called after the load has taken place, isn't it?

Condor
24 Feb 2011, 3:56 AM
1. 'readRecords' is called when the server responds with data and that data needs to be converted to records. My code calls the original readRecords, but returns the modified records and a different totalRecords instead.

2. Adding and removing records doesn't modify the 'totalLength' property of the store (I've reported this as a bug, but it was dismissed because they think you shouldn't modify a paged store). store.getCount() does return the correct number of records though.

raj_plays
24 Feb 2011, 9:49 AM
That was enlightening.
Cheers, Condor.