PDA

View Full Version : store getById not working?



profunctional
8 Feb 2011, 1:21 PM
What is the trick to getting this to work? I set the idProperty of my model to ID. Then I try to use store.getById(ID) and it always returns null. What else is needed for this to work.

I know the ID is in the store because I can get the equivalent result from doing something like this

itemStore.each(function(rec){
if(rec.get('ID') == id)
{
item = rec;
return;
}
});
i'd rather not loop through each item to retrieve the record though. Seems like getById should suffice.

evant
8 Feb 2011, 1:47 PM
There's no trick. Did you set the idProperty on the Model?

profunctional
8 Feb 2011, 1:53 PM
Yes, like this:


Ext.regModel('ItemModel', {
idProperty: 'ID',
fields:[{name: 'ID', type: 'int'},
{name: 'Type', type: 'string'},
{name: 'ItemTypeId', type: 'int'}
],

});

niu01
22 Feb 2011, 7:44 PM
I have the same problem.
Could you tell me how to fix it?

bwg
23 Feb 2011, 4:05 PM
I don't know if it's the same problem, but I ran into the situation where



store.load(); // store uses an ajax proxy
modelInstance = store.getById(id); // modelInstance = object
// add new record
store.load();
modelInstance = store.getById(newId); // modelInstance = null
a more reliable method is:


store.getAt(store.findExact('id', id));

mitchellsimoens
23 Feb 2011, 5:30 PM
Here is the source for getById:


getById : function(id) {
return (this.snapshot || this.data).findBy(function(record) {
return record.getId() === id;
});
},

Basically it goes through each record and calls it's getId method so you have to make sure the id you are passing to the getById is exactly the same. If your IDs are numbers, you have to send it as a number. Same thing goes for if they are strings but you have your Model set up to change it into a number.

Are you sure you are sending the correct type?

bwg
23 Feb 2011, 7:22 PM
explicitly casting the id to the declared type, in this case .getById(parseInt(id)), had no effect on the outcome. The call still returned null after the second load().

joelennon
4 Mar 2011, 6:34 AM
Had this issue aswell, had a form that saved a new entry to the database, then reloaded the store, could not retrieve the new record from the store using getById (but could retrieve all other records).

bwg's suggested solution:


store.getAt(store.findExact('id', id));

worked nicely for me.

MaciejZabielski
15 Feb 2012, 5:52 AM
I have lost (again) somany neves at ST... My case is exaclty the same, only records inside snapshot will be found!

HdMobile.views.viewer.documentGrid.getStore().getById("452345234")
null
HdMobile.views.viewer.documentGrid.getStore().getById(452345234)
null
HdMobile.views.viewer.documentGrid.getStore().findExact('id', '452345234')
0