View Full Version : Visibility of Ext.define - variables

10 Jan 2012, 4:30 AM
Hi guys,

happy new year.

I have here a simple code, in which I want to save some data from serverside in a singleton.

Ext.define('LoginData', {singleton:true,loggedIn: false});
url: 'data/testLoginHandler.php',
success: function(response){
LoginData.loggedIn = response.responseText;
alert(LoginData.loggedIn + ' ' + response.responseText);

The output of this code is:
true true

Can anyone tell me, why the action in the success-method is not visble outside?

Thanks a lot,

10 Jan 2012, 4:42 AM
The problem has nothing to do with visibility, it's timing.

The Ajax request is asynchronous. It doesn't block the current thread. Execution of your code will continue while the request is ongoing. The success callback won't be called until after the current thread yields and the HTTP request is complete.

I highly recommend avoiding the use of alert for debugging issues like this. alert totally mucks up the JavaScript threading model and can result in strange behaviour that isn't possible under any other circumstances. Use console.log instead and observe the order that the lines are logged, I suspect you'll see that the two 'false' lines get logged before the 'true true'.

10 Jan 2012, 5:11 AM
Ah, I see,

is there any possibility to stop the process until this essential request was made?
Or I think I saw a property named "sync" somewhere?

Thanks again,

10 Jan 2012, 5:18 AM
Yes, you could make it synchronous, but you shouldn't. The browser will seize up until the request is complete if you do that. Asynchronous programming is a paradigm you need to embrace rather than fighting it. If you need to wait for the request to complete before proceeding then move the logic into the callback.

If this information is loaded when the page loads then consider injecting the data into the page using a server-side technology (JSP, ASP, PHP, etc.). That way you won't need to make an extra request to grab it in the first place.

10 Jan 2012, 5:22 AM
I think you are right. I have to put the js-file in a php-file and handle the login-process in php.

But how could I synchronize it (without using "while (! LoginData.loggedIn) {})?