PDA

View Full Version : store.getCount() return 0



sterin
27 May 2011, 1:07 AM
This is a nervous problem :P

I have a simple Store like this:

store = new Ext.data.Store({
model: 'model',
autoLoad:false,
proxy: {
type: 'ajax',
url : 'file.xml',
reader: {
type : 'xml',
root : 'models',
record: 'model'
}

}
});

but i can't understand why store.getCount() always return me 0! Why?!?!

mitchellsimoens
27 May 2011, 5:04 AM
Because you don't have any data in your Store. Make sure the root and fields match.

sterin
27 May 2011, 5:10 AM
Because you don't have any data in your Store. Make sure the root and fields match.

i use this store to fill a list and i see all the data! But the GetCount() give me 0!

mitchellsimoens
27 May 2011, 5:49 AM
Are you looking for getCount before the load event or after the load event was fired?

sterin
27 May 2011, 6:13 AM
after.. it's like that:

this.MyStore = new Ext.data.Store({
model: 'MyModel',
autoLoad:true,
proxy: {
type: 'ajax',
url : xmlurl
reader: {
type : 'xml',
record: 'MyRecord'
}
}
});
this.MyList = new Ext.List({
baseCls: 'list',
grouped: true,
indexBar: true,
emptyText: 'No Results',
itemTpl: MyTplTemplate,
store: MyStore
});

alert(this.MyList.getStore().getCount());
//or
alert(this.MyStore.getCount());

Thanks

mitchellsimoens
27 May 2011, 6:35 AM
Your JavaScript is being executed before the Store's call is actually returned. Store calls are asynchronous so while the Store's AJAX call is running, the JavaScript continues to be called.


this.MyStore = new Ext.data.Store({
model: 'MyModel',
autoLoad:true,
proxy: {
type: 'ajax',
url : xmlurl
reader: {
type : 'xml',
record: 'MyRecord'
}
},
listeners : {
load : function(store) {
alert(store.getCount());
}
}
});

I have highlighted my change in red. The 'load' event will fire once the Store's AJAX call has returned and the data has been entered. This will now alert the correct number.

sterin
27 May 2011, 6:44 AM
YESS!!!! Now it's perfect :D:D

Thank you so much!!

mrunal
12 Mar 2012, 6:10 AM
Hey what if my Store is defined in some file say Store.js and i want to use getCount() in my controller?

mitchellsimoens
12 Mar 2012, 6:18 AM
Use the on method to add a listener to the store instance

mrunal
12 Mar 2012, 6:40 AM
i create store instance as follows:

var store1= Ext.create('Sencha.store.UserStore');

what next?

mitchellsimoens
12 Mar 2012, 6:43 AM
var store1 = Ext.create('Sencha.store.UserStore', {
listeners : {
load : function(store) {
console.log(store.getCount());
}
}
});

mrunal
12 Mar 2012, 8:19 PM
Hey michel,

Thanks for the suggestion.
It works if i display in load function.

But, i want to get the count of number of records and store it in a variable say cnt.

Is it possible?

I'm trying this and it displays undefined. :(



var cnt;
var store1 = Ext.create('Sencha.store.FaStore', {
listeners : {
load : function(store) {
//console.log(store.getCount());
cnt=store.getCount();
}
}
});

console.log(cnt);

mitchellsimoens
13 Mar 2012, 3:46 AM
Once again, you are not thinking async. The load listener will fire after your console.log(cnt). Put that within the load listener and it will work.

hjeDK
17 Jul 2012, 4:27 AM
How do you set up a listener to a stores load event in the control-config of a controller?
I've got it going by adding the suggested listener config in the store itself, but I would like to have the listener in my controller instead. but I fail to set it up right. My guess would be like this:


'<storeId>'. {
load: 'someFunctionName'
}
But that doesn't work.
Any ideas anybody??