1. #1
    Ext JS Premium Member
    Join Date
    Mar 2007
    Location
    IL, USA
    Posts
    141
    Vote Rating
    0
    tookoo is on a distinguished road

      0  

    Default How to make synchronous AJAX request using Ext library?

    How to make synchronous AJAX request using Ext library?


    I checked the API of Connection/AJAX classes, but cannot find related configuration options? Can someone help me out here? Thanks.

  2. #2
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,505
    Vote Rating
    53
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      -1  

    Default


    As has beeb mentioned many times. Synchronous operation is neither supported nor desirable. Use callbacks to continue processing after the return of data.

  3. #3
    Ext User
    Join Date
    May 2007
    Posts
    45
    Vote Rating
    1
    cwolves is on a distinguished road

      0  

    Default


    Do it manually by creating a new xmlhttp object. I've only ever run into ONE situation where I actually found a synchronous xmlhttp connection to be useful, however - dynamically loading javascript code.

  4. #4
    Ext JS Premium Member
    Join Date
    Mar 2007
    Location
    IL, USA
    Posts
    141
    Vote Rating
    0
    tookoo is on a distinguished road

      0  

    Default


    Quote Originally Posted by Animal View Post
    As has beeb mentioned many times. Synchronous operation is neither supported nor desirable. Use callbacks to continue processing after the return of data.
    For example, what if I want to make several AJAX calls to download multiple XML files before the page is displayed? When using asynchronous calls to get those XML files, I have to call load_xml_b() inside the callback function of load_xml_a() .... Then, there is no single place to view the whole program logic.... Since I don't think customers will care what happens before the page is displayed, Using synchronous AJAX calls here may be helpful for a cleaner program logic.

  5. #5
    Sencha User
    Join Date
    Mar 2007
    Posts
    7,854
    Vote Rating
    4
    tryanDLS is on a distinguished road

      0  

    Default


    Quote Originally Posted by tookoo View Post
    For example, what if I want to make several AJAX calls to download multiple XML files before the page is displayed? When using asynchronous calls to get those XML files, I have to call load_xml_b() inside the callback function of load_xml_a() .... Then, there is no single place to view the whole program logic.... Since I don't think customers will care what happens before the page is displayed, Using synchronous AJAX calls here may be helpful for a cleaner program logic.
    That's a flawed approach. Similar to people that think 1000 line VB6 functions are good b/c you can 'view the whole program logic'. The reality is that you're doing event driven programming. This is not an Ext 'thing' - it's advanced UI/clientside coding in general. Think about how you handle UI interaction - there's no single place to read thru the the flow - events are fired and the flow jumps all over.

    You don't have to trigger the 2nd request from the callback of the 1st unless you need to chain the requests, or stop subsequent requests after a previous failure. You could just kick off the 2 requests - they each trigger their callback fn when they're done and you go on. That may mean that you have to keep track of whether both of them finish before you do something else, but that's part of the process.

    As another example (and I'm not suggesting this would be a good idea in general), what if you had 10 files to load - do you really want to chain 10 requests and manage that complexity? Or do you want to just fire 10 requests, let the browser manage sending them (2 at a time, or whatever the http limit happens to be)? Let them trigger their callbacks as they complete and then move on.

  6. #6
    Ext User
    Join Date
    May 2007
    Posts
    45
    Vote Rating
    1
    cwolves is on a distinguished road

      1  

    Default


    as tryanDS said, your approach is fundamentally flawed. If you want to do this, and you want to make sure that all your requests are loaded before the page loads:

    PHP Code:
    Ext.Ajax.request({url'file1.xml'callbackmyCallback});
    Ext.Ajax.request({url'file2.xml'callbackmyCallback});
    Ext.Ajax.request({url'file3.xml'callbackmyCallback});
    Ext.Ajax.request({url'file4.xml'callbackmyCallback});
    Ext.Ajax.request({url'file5.xml'callbackmyCallback});
    Ext.Ajax.request({url'file6.xml'callbackmyCallback});
    Ext.Ajax.request({url'file7.xml'callbackmyCallback});
    Ext.Ajax.request({url'file8.xml'callbackmyCallback});
    Ext.Ajax.request({url'file9.xml'callbackmyCallback});
    Ext.Ajax.request({url'file10.xml'callbackmyCallback});

    function 
    myCallback(){
        if(++
    this.counter<10) return;

        
    // all files are done loading, do stuff
    }
    myCallback.counter 0

  7. #7
    Sencha User
    Join Date
    Apr 2012
    Location
    Austin, Texas
    Posts
    2
    Vote Rating
    0
    brian.moeskau is an unknown quantity at this point

      -1  

    Default


    AJAX == Asynchronous JavaScript And Xml

  8. #8
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,505
    Vote Rating
    53
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    Quote Originally Posted by tookoo View Post
    For example, what if I want to make several AJAX calls to download multiple XML files before the page is displayed? When using asynchronous calls to get those XML files, I have to call load_xml_b() inside the callback function of load_xml_a() .... Then, there is no single place to view the whole program logic.... Since I don't think customers will care what happens before the page is displayed, Using synchronous AJAX calls here may be helpful for a cleaner program logic.
    It's important to be able to use OO principles rather than huge monoliths of code.

    If you break up your in-page code into logically separate objects, then each one can perform its own Ajax request to fetch data, and then call some callback which it is given.

    Each object does not have to know about any other chained Ajax request, it's just given a function to execute.

    Your application logic chains these together.

    For example, to create a Grid, I have three chained Ajax requests.

    The constructor sends a query to the server which responds with success/fail. On return of that data, a request for the table's metadata is sent, and on return of that data, a ColumnModel and Record are created. Then a request is sent for the first page of data. On return of that, the grid is rendered.

    At no point is the browser in a wait state.

  9. #9
    Sencha User
    Join Date
    Mar 2007
    Posts
    29
    Vote Rating
    0
    cchiriac is on a distinguished road

      0  

    Default Asynchronous vs. Sequenced

    Asynchronous vs. Sequenced


    I think there a small confusion here. I don't know if my point of view is correct but, synchronous requests mean for me:
    req1 // JS thread block
    req2 // JS thread block
    ...
    reqn // JS thread block

    Making the requests asynchronouslly means all requests are made virtually in the same time (depending on the browser's strategy) or, more true, that no sequencing is guaranteed.

    This can make the order in which the requests were called to be different that the one they are actually proccesed and the responses received.

    I think the notion of sequenced asynchronous requests can exist as beeing a part of some batching scheme having both the non-blocking advantage of asynchronous and the control (sequencing can be just an example, other strategies could be implemented).

    I've been working on a proxy that allows to do just that, it's in a really early stage (this is why i haven't posted anythig yet) that you can see at :

    http://www.fullwww.com/sandbox/Seque...Proxy_PACK.php
    It could be a bit slow at init because it's creating lots of gris.


    And the downloads:
    * the proxy :
    http://www.fullwww.com/sandbox/lib/f.../seq_proxy.tgz
    * the whole example (with php):
    http://www.fullwww.com/sandbox/SequencedHttpProxy.tgz

    Suggestions are more than wellcome. I know quality of the code is really poor, sorry in advance. As i'll hopefullly get better in JS i'll be correcting the code. Some small doc is on its way too.

  10. #10
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,505
    Vote Rating
    53
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    This can make the order in which the requests were called to be different that the one they are actually proccesed and the responses received.
    Unless you sequence them by calling the next one in a callback that is run when the data has been returned.