PDA

View Full Version : Using findRecord() on stores, why am i getting null result?



AesonErryl
17 Oct 2012, 10:51 PM
Have I forgotten something here?

Contoller:


Ext.define('Tools.TimeTracker', {
extend: 'Ext.app.Controller',
stores: [
'TimetrackerProjects'
],
//...some codes here


console.log(this.getTimetrackerProjectsStore());
//the store has currently 4 records; as seen on the image below

console.log(this.getTimetrackerProjectsStore().findRecord('projectid', 'be65deda-4fd8-46db-8543-ea563158fbdb', 0, false, false, true));
//but returns null.

console.log(this.getTimetrackerProjectsStore().findRecord('projectid', 'be65deda-4fd8-46db-8543-ea563158fbdb'));
//this also returns null.

//I am sure I've supplied the right fieldName and value.


//...more codes below



39449

Farish
17 Oct 2012, 11:24 PM
are you calling the findRecord() after the store has been loaded? try this:


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

it might happen that console.log prints the store after it has been loaded but since the records were not there in the store when findRecord was called, it returns null.

AesonErryl
17 Oct 2012, 11:38 PM
are you calling the findRecord() after the store has been loaded? try this:


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

it might happen that console.log prints the store after it has been loaded but since the records were not there in the store when findRecord was called, it returns null.


Thanks Farish,

But I cannot use "store.on('load', ...);" since I only need to load my store once. Once the store is loaded, I need to run multiple searches over my store using a for-loop. The record I need depends on the projectID i have.

Farish
17 Oct 2012, 11:42 PM
then how will you know if your store is loaded or not? if the store is not loaded, then findRecord will always return null. even if its loaded just once, you can still add a listener. this will ofcourse fire only once because the store is loaded just once in your app.

scottmartin
19 Oct 2012, 7:05 AM
You need to make sure your data is loaded. It may simply be a timing issue:



var store = Ext.create('Ext.data.Store', {
storeId : 'simpsonsStore',
fields : ['name', 'email', 'change'],
data : {'items' : [
{ 'name' : 'Lisa', 'email' : 'lisa@simpsons.com', 'change' : 100 },
{ 'name' : 'Bart', 'email' : 'bart@simpsons.com', 'change' : -20 },
{ 'name' : 'Homer', 'email' : 'home@simpsons.com', 'change' : 23 },
{ 'name' : 'Marge', 'email' : 'marge@simpsons.com', 'change' : -11 }
]},
proxy : {
type : 'memory',
reader : {
type : 'json',
root : 'items'
}
}
});

var rec = store.findRecord('name', 'Bart');
console.log(rec);



Scott.