PDA

View Full Version : Help with findExact method for a Store, not returning correct index



alanp
7 Dec 2011, 1:56 PM
Hi All,

I'm fairly new to Sencha and so might be missing some pieces (sorry in advance) but can't seem to figure out this problem:

I testing out some concepts and have a test application. All I'm doing is loading a store via data from a remote server using a scripttag proxy, then finding the index of the record that corresponds to a upid=0 ( i.e. findExact('upid', 0) ), and outputting that index to the console window (I'm using Chrome developer tools).

When the findExact method is ran within the code after the store load, the index returned is -1, so seems like it can't find it. However, when I run the exact same findExact method from the console terminal manually, it returns 2 which is the correct index of the record I'm searching for.

Any ideas on what I'm doing wrong? I can't understand why I'd get two different results using the exact same line of code. Thanks in advance.

Here is my code:

Model:


Ext.regModel('UserModel', {
fields: [
{
name: 'uid',
type: 'int'
},

{
name: 'firstname',
type: 'string'
},
{
name: 'lastname',
type: 'string'
},

{
name: 'upid',
type: 'int'
},
{
name: 'count',
type: 'int'
},
],


});


Here is my Store:



Ext.regStore('UserStore', {
model: 'UserModel',
autoLoad: false, //I only want it to load when I call it.
proxy: {
url: 'http://www.somedomain.com/api/user?action=get', //I've verified it's returning and storing
type: 'scripttag', //cross-domain AJAX
reader: {
type: 'json',
root: 'data'
}
},
});
App.stores.userStore = Ext.StoreMgr.get('UserStore');


My Controller:



Ext.regController('User', {


index: function(options)
{
Ext.StoreMgr.get('UserStore').load();
console.log( Ext.StoreMgr.get('UserStore').findExact('upid', 0);
},

});




Url: mydomain.com/#user/index

Console window:

> -1
> Ext.StoreMgr.get('UserStore').findExact('upid', 0);
> 2

mitchellsimoens
9 Dec 2011, 11:07 AM
Doing exactly this:


Ext.StoreMgr.get('UserStore').load();
console.log( Ext.StoreMgr.get('UserStore').findExact('upid', 0);

Will always return -1. The reason is that the store loading is asynchronous and so therefore you have to use a callback or listen to the load event until you can try to search it:


var store = Ext.StoreMgr.get('UserStore');

store.on('load', function(s) {
console.log( s.findExact('upid', 0) );
});

store.load();