Results 1 to 5 of 5

Thread: Visibility of Ext.define - variables

  1. #1
    Sencha User
    Join Date
    Feb 2010
    Posts
    82
    Answers
    2

    Default Answered: Visibility of Ext.define - variables

    Hi guys,

    happy new year.

    I have here a simple code, in which I want to save some data from serverside in a singleton.
    PHP Code:
    Ext.define('LoginData', {singleton:true,loggedInfalse});
        
    alert(LoginData.loggedIn);
        
    Ext.Ajax.request({
            
    url'data/testLoginHandler.php',
            
    success: function(response){
                
    LoginData.loggedIn response.responseText;
                
    alert(LoginData.loggedIn ' ' response.responseText);
            }
        });
        
    alert(LoginData.loggedIn); 
    The output of this code is:
    false
    true true
    false

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

    Thanks a lot,
    dobberph

  2. 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'.

  3. #2
    Sencha Premium Member skirtle's Avatar
    Join Date
    Oct 2010
    Location
    UK
    Posts
    3,791
    Answers
    585

    Default

    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'.

  4. #3
    Sencha User
    Join Date
    Feb 2010
    Posts
    82
    Answers
    2

    Default

    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,
    dobberph

  5. #4
    Sencha Premium Member skirtle's Avatar
    Join Date
    Oct 2010
    Location
    UK
    Posts
    3,791
    Answers
    585

    Default

    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.

  6. #5
    Sencha User
    Join Date
    Feb 2010
    Posts
    82
    Answers
    2

    Default

    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) {})?

    Thanks,
    dobberph

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •