PDA

View Full Version : How does Jsonstore.getById() works ?



ajay.kumar2
6 Jan 2011, 12:25 PM
Hi,

I am not able to access the records in the store using Jsonstore.getById() , I get 'undefined' .
Can someone help me on this ?

fay
6 Jan 2011, 12:53 PM
Without seeing any of your code, I'd suggest using ext-all-debug.js, FireFox and FireBug. Put a breakpoint at the line that calls getById() and step into the code to see where it is going amiss.

faith wins
6 Jan 2011, 1:16 PM
Store.getById(recordId) gets you the record with the specified recordId. When you create the store, you would set its IdProperty to one of the fields in the store's data.
e.g

var js = new Ext.data.JsonStore({
root: 'employees',
fields : [{name: 'empId', type: 'int'},{name: 'firstName', type: 'string'}, {name: 'lastName', type: 'string'}, {name:'title', type: 'string'}],
idProperty: 'empId',
data: {"employees": [{empId : 1 , firstName : 'Abebe' , lastName : 'Kebede' , title : 'Developer' }, {empId : 2 , firstName : 'Belachew' , lastName : 'Tariku' , title : 'Designer' }, {empId : 1 , firstName : 'Elsa' , lastName : 'Alemu' , title : 'Analyst' } ]}
}); Now the following will get you the record object with empId equal to 1,
js.getById(1) which is
{empId : 1 , firstName : 'Abebe' , lastName : 'Kebede' , title : 'Developer' } I hope that helps!

ajay.kumar2
6 Jan 2011, 2:39 PM
My code is something like this:-

//Store definition


var js = new Ext.data.JsonStore({

fields : [{name: 'empId', type: 'int'},{name: 'firstName', type: 'string'}, {name: 'lastName', type: 'string'}, {name:'title', type: 'string'}],
idProperty: 'empId'
}
});

I add records to store:-

var newrec = Ext.data.Record.create(fields); //fields same as above in store

js.add(new newrec({empId : 1 , firstName : 'Abebe' , lastName : 'Kebede' , title : 'Developer' });

js.commitChanges();

So now when i do a js.getById(1) //using empId i get undefined.

i checked the id attribute of record using js.getAt(0).id , the value comes to be something "ext-record-123"

i guess the id's are automatically getting generated.

Any help is greatly appreciated.

fay
6 Jan 2011, 4:37 PM
You're right, the id will be auto-assigned if you don't specify it. You should be able to fix up your code from looking at the example in the create (http://dev.sencha.com/deploy/dev/docs/?class=Ext.data.Record) method in the Ext.data.Record docs.



var TNewRecord = Ext.data.Record.create(fields); //fields same as above in store

var newRec = new TNewRecord(
{
empId : 1 ,
firstName : 'Abebe' ,
lastName : 'Kebede' ,
title : 'Developer'
},
1 // optionally specify the id of the record otherwise one is auto-assigned (http://dev.sencha.com/deploy/dev/docs/output/Ext.data.Record.html#Ext.data.Record-Record.id)
);

js.add(newRec);