PDA

View Full Version : [SOLVED] Howto change a variable by callback



ctp
3 Nov 2010, 7:31 AM
What's the way to change a variable by a callback function? I'm using the sm listener 'rowselect' and wanna change one of the records within the callback function:



listeners: {
rowselect: {
fn: function(sm, rowIdx, r) {
var pcstore = new App.store.PropertyClassStore;
pcstore.load({
callback: function(){
r.data['use_type_id'] = 999;
}
});
console.dir(r.data);
}
}In my case r.data['use_type_id'] is not set to the new value.

plalx
3 Nov 2010, 7:43 AM
You are probably outputing the data in console before the load is finished. Loading is asynchronous...
Try moving console.dir(r.data); into the callback function as well.

Condor
3 Nov 2010, 7:46 AM
You need to learn the concept of event driven programming.

The call to store.load() will just finish and any code after that will be executed (like your console.dir).

When the data is actually received from the server, the callback will be executed, but by then the rowselect function will already have finished.

Any code you wish to execute that relies on the received data will need to be inside the callback function (or called by the callback function).

ctp
3 Nov 2010, 7:48 AM
Thanks ;-)