1

How can the value of variable 'updated', be modified in the code below from within callback.

Why the variable 'updated' is not getting modified in the following manner (i.e. it returns false every time function is invoked)?

updateData:function()
{
    var updated = false;
    var store = new Ext.getStore('MyStore');
    store.load({
        scope: this,
        callback: function(records, operation, success) {
         if(/*some condition*/){
            updated=true;   
          }
        }
    });

return  updated ;
}
A. Sinha
  • 2,666
  • 3
  • 26
  • 45
  • I guess this function return false every time because `store.load()` (I assume that you use remote proxy) is asynchronous call and you just define `update` variable, call `load` and return `update` variable. So store is not loaded yet and `update` is still false. – Sergey Novikov Mar 27 '16 at 16:26
  • @SergeyNovikov yes you r right, I am using a remote proxy – A. Sinha Mar 27 '16 at 16:39
  • Well, I described how ur code sample actually work. `updated` variable is changed to true, but when `callback` function is fired. – Sergey Novikov Mar 27 '16 at 17:15
  • @SergeyNovikov how can I return true from the above piece of code, because it is still returning false – A. Sinha Mar 27 '16 at 17:32
  • You can't. At the point the function returns, the callback has not been triggered. You can see this behaviour using a debugger. – Robert Watkins Mar 27 '16 at 20:59
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Evan Trimboli Mar 28 '16 at 09:11

1 Answers1

1

Not sure what exactly you want to achieve with this piece of code, but since store.load() is asynchronous (I assume that you use remote proxy like ajax) store is not loaded when you return updated so its still false.

If you just want do something when store is loaded you can do it within callback anonymous function (or pass callback function to your updateData function and call it within callback?).

If you want to check if store is loaded or not you can call isLoaded() for ExtJS 5+ or check for store lastOptions property (if that exists then your store has loaded at least once) property for ExtJS 4.

I do not see adequate solutions to your updateData function returns true.

Sergey Novikov
  • 4,096
  • 7
  • 33
  • 59