PDA

View Full Version : extjs4 to check store loaded



satya1974
23 Aug 2012, 8:06 PM
I have a combobox which I am loading from a store. I want to check if the store is not loaded with data i want to disable few fields and if it is loaded with data i want to enable few fields. please find below my store as well as my load function. The problem is first time always store success is returning me false and after that it returns true. please help me where i am doing wrong.
// model
Ext.define('Company', {
extend: 'Ext.data.Model',
fields: [
{name: 'companyId'},
{name: 'companyName', type: 'string'}
],
idProperty: 'companyName'
});
// create the data store
var companyStore = Ext.create('Ext.data.Store', {
storeId: 'companyStore',
model: 'Company',
proxy: {
type: 'ajax',
url: 'master/viewCompanies.action',
reader: {
type: 'json',
root: 'data',
totalProperty: 'total'
}
}
});
companyStore.load(
{
params:{accountId:formPanel.getForm().findField('accountId').getValue()},
scope: this,
callback: function(records,operation,success){
if(success){
alert('1');
}//end if
else
{
alert('2');
}
}//end callback
});

kislay.kishore
24 Aug 2012, 1:23 AM
I think you need to add a load listener here, please see if the below code/logic helps.




var companyStore = Ext.create('Ext.data.Store', {
storeId: 'companyStore',
model: 'Company',
//autoLoad: true,
proxy: {
type: 'ajax',
url: 'master/viewCompanies.action',
reader: {
type: 'json',
root: 'data',
totalProperty: 'total'
}
},
listeners: {
load: function(store) {
alert('Number of records in store'+ store.count());
iterateStore(store); //function to iterate the store and do further task
}
}
});
/////////////
funtion iterateStore(myStore){
//Here r means record in store
myStore.each(function(r) {

var id = r.data['companyId'];
var name = r.data['companyName'];

//further enable/disable logic goes here
});
}

satya1974
24 Aug 2012, 1:47 AM
First of all thank you very much for your reply but here the problem is the first time (when the browser opened with url) when the store get loaded it will say count : 0 though there are 2 and if i click on combo it is also showing 2 records. From the second time it gives me correct count : 2, so i want to understand why count is comming as 0 first time and from there onwards it comes 2.
quick help is highly appreciated.

kislay.kishore
24 Aug 2012, 5:04 AM
Try passing some default paramters (params config in the Store) and set autoLoad:true.

I think, load is not called on the first load (onReady) somehow.

However, it beats me that store count is zero and record shows up in dropdown!!

satya1974
24 Aug 2012, 6:15 AM
actually i have to pass parameter then only i have to load the store.

kislay.kishore
24 Aug 2012, 9:40 AM
Ok....

What I get from you code and your last post that the dropdown store's params are coming from some other dropdown/component....then in that case ....if I would have been you...i would have kept dropdown disabled until the store gets loaded with some correct list of values, (which means something to the end-user) and once it is loaded then only enable the dropdown.

Well, I dont mean that you should alter you UI design/flow....but its just a suggestion...take it if you feel like..coz anyway if there is dropdown which is enabled, a user might anyway try to select something...

Hope you understand my point here...please let me know if you some questions or something wrong in my understanding, I will try to help you further.

satya1974
26 Aug 2012, 7:56 PM
thank you very much for your reply but my concern was that only. I am already putting the alert to check the store is loaded or not and based on that i want to enable/disable drop down. But here the problem is the very frist time the store count is appearing as 0 (even i tried with success parameter which returns false) though there are records that json object holding. If i click again submit button (second time) to store the load it returns correct values of count as well as success parameter. For me the main problem was first time when the store get loaded.