PDA

View Full Version : adding copied records does not update idIndex in ArrayReader.



ccrotty
22 Jul 2010, 11:10 AM
If I run the sample code below, the getById() method on tstStore2 fails to find the record. Looking at the tstStore2 data in Firebug, it seems as if the records kept the idIndex defined from the store they were copied from. So the copied records in tstStore2 still have the id (idIndex 0) set.

Is there a call needed beyond store.add() to get the indexes to update?



Ext.onReady(function()
{
var tstData = [[1,'Name1'],[2,'Name2'],[3,'Name3']];

var tstStore = new Ext.data.ArrayStore(
{
autoDestroy: true,
storeId: 'testStore',
idIndex: 0,
fields:
[
{ name: 'id', type: 'int' },
{ name: 'nm', type: 'string' }
]
});

var tstStore2 = new Ext.data.ArrayStore(
{
autoDestroy: true,
storeId: 'testStore',
idIndex: 1,
fields:
[
{ name: 'id', type: 'int' },
{ name: 'nm', type: 'string' }
]
});

tstStore.loadData(tstData);

// Now can we get by Id
var testID = 2;
var aRecord = tstStore.getById(testID);

if (aRecord != null)
alert(aRecord.get('nm') + ' was found by id ' + testID);
else
alert('record by id ' + testID + ' not found.');

// Copying Records into new store leaves same index information
var records = tstStore.getRange();

Ext.each(records,
function(r)
{
// Copy records for data store
tstStore2.add(r.copy());
},
this
);

// Now can we get by second index?
var testID2 = 'Name3';

// ***** The following method fails since record seems to still show idIndex: 0 as id ******
var aRecord2 = tstStore2.getById(testID2);

if (aRecord2 != null)
alert(aRecord2.get('nm') + ' was found by id ' + testID2);
else
alert('record by id ' + testID2 + ' not found.');

});

Animal
22 Jul 2010, 11:47 AM
Why would the Record change its ID upon copy? To what? How?

If you want to change its ID, pssthe new ID into the copy call as described in the API docs.

ccrotty
22 Jul 2010, 2:30 PM
I got around this by setting the record.id directly. I'm not sure if will cause any other issues, but seems to work.


Ext.each(records,
function(r)
{
// Copy records for data store
var r2 = r.copy();
r2.id = r.get('nm');
tstStore2.add(r2);
},

Animal
22 Jul 2010, 2:32 PM
Did you read the API docs for copy as I suggested???