PDA

View Full Version : Gird store onload fired multiple times (updated)?



duskandawn
15 Sep 2010, 4:27 AM
(not sure why my previous posts didnt get publish at all, trying again)

Considering the same grid :



Ext.ns('Test.grid');

Test.grid.searchGrid = Ext.extend(Ext.grid.GridPanel, {
title: Test',
initComponent: function() {
var config = {
id: 'searchgrid',
title : 'Data',
hidden : true,
frame:true,
store: this.buildStore(),

};
Ext.apply(this, Ext.apply(this.initialConfig, config));
Test.grid.searchGrid.superclass.initComponent.apply(this, arguments);
},

buildStore : function(){
return new Ext.data.Store({
proxy: new Ext.data.HttpProxy(callRequest({
service : "TestRecord"
})),
reader: new Ext.data.JsonReader({
totalProperty: 'count',
root : 'record',
fields: [
{name:'acctId'},
{name: 'prdTyp'},
]
}),

listeners: {
load: function(store,record,options)
{
alert("called from listener");
}
}
});
// the below loadGrid is called from another js file as Ext.getCmp('searchGrid').loadGrid(test)
loadGrid : function(data){
this.store.load({
params:{
start:data,
limit:10
}
});
this.store.on('load', function() {
alert("called from outside");
});

}
}



What i am seeing is, if i call the loadGrid() function multiple times from the other panel js file which hosts this grid,
i see the alert("called from outside"); call being called multiple times. i.e for the first time when i call, i see the alert coming up one time,
the next time when i call the loadGrid, i see the alert coming up 2 times.

But this doesnt happen with the alert which is present under the store listener, the alert present there is only called once, no matter how many time i load the store.

Can you please help what is going on here.
In the broader extent, i am reaching to the point that, my complex code running after on-load event in the loadGrid, i may have to move it under listener.
But just wanted to know the reasoning behind this phenomena,

Please advise,

Condor
15 Sep 2010, 5:57 AM
You are registering a new load handler every time you call the function.

You want:

this.store.load({
params:{
start:data,
limit:10
},
callback: function(){
alert('I am only called after this load is complete');
}
});
or

this.store.on('load', function() {
alert("This handler is removed after being called");
}, this.store, {single: true});

duskandawn
15 Sep 2010, 6:13 AM
Thanks Condor,
That makes perfect sense.
Will always keep this in mind from now on.
But how does callback() differs from this.store.onLoad event ?

Condor
15 Sep 2010, 8:49 AM
Callback is called after the load event (can be useful sometimes) and it also called for failed loads (which would trigger the exception event).