PDA

View Full Version : scope of Load



djfloetic
20 Jul 2007, 10:39 AM
I'm trying to basically acquire data from the database via a JSON request. There are multiple stores involved and have callbacks setup to call another callback once the data has been requested.

The first store loads and displays data on a grid fine but the 2nd store: 'statsDS' doensn't get to load properly for some reason, and maybe I'm just confused with the scoping.


This is the first store that gets loaded, notice that Callback calls 'generateStatsDS()' once it is done.

// trigger the data store load
ds.load({params:{func: 'viewAllApprovalRequired', start:0, limit:30, userRole: 'security'}},
{callback:generateStatsDS()});
ds.baseParams.func = "viewAllApprovalRequired";



This is the call back function that gets called once it is finished with previous data load.


var statsDS;

function generateStatsDS() {
// create the Data Store
// App Control Combo Data Store
statsDS = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({
url: './includes/database/dbmanager.php'
}),

// create reader that reads the records
reader: new Ext.data.JsonReader({},['numMembers', 'numClasses'])
});
statsDS.baseParams.func = "getStats";
statsDS.load({params: {func: 'getStats'}}, {callback:renderStats()}, {scope:this});

}

//* r : Ext.data.Record[]
//* options: Options object from the load call
//* success: Boolean success indicator

Also below, it makes a callback to the 'renderStats()' function once this load is complete.

function renderStats(r, options, success) {
var numMembers = statsDS.getAt(0).data.numMembers;
var numClasses = statsDS.getAt(0).data.numClasses;

// Render statistics
Ext.get('numMembers').dom.innerHTML = numMembers;
Ext.get('numClasses').dom.innerHTML = numClasses;
}

However, my problem is that from a scoping standpoint, parameters: r, options, success have no value and aren't defined as stated in the API docs: http://extjs.com/deploy/ext/docs/output/Ext.data.Store.html#load

Is the Scoping correct or am I missing something. While stepping through with firebug, it is not defined.

Thoughts? Ideas?

DigitalSkyline
20 Jul 2007, 10:56 AM
My guess would be : {callback:renderStats()}, should be {callback:renderStats}.

renderStats() is firing the function instead of passing the object reference... make sense?

djfloetic
20 Jul 2007, 11:10 AM
That could be it, but for some reason, it seems to never fire.

I have an alert("renderStatsCalled"); in the function to alert when it actually fires the callback.

It appears that it doesn't. =(

tryanDLS
20 Jul 2007, 11:16 AM
Set a BP on this line - what is 'this' pointing to? Is renderStats visibible in that scope?

statsDS.load({params: {func: 'getStats'}}, {callback:renderStats}, {scope:this});

DigitalSkyline
20 Jul 2007, 11:30 AM
//Instead of this:
statsDS.load({
params: {
func: 'getStats'
}
},
{
callback:renderStats
},
{
scope:this
});


//Maybe this:
statsDS.load({
params: {
func: 'getStats'
},
callback:renderStats,
scope:this
});

djfloetic
20 Jul 2007, 11:54 AM
that worked.

Thanks DigitalSkyline.

djfloetic
20 Jul 2007, 11:55 AM
Thanks Ryan, I found my problem.

Ext rocks. =)