Hybrid View

  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,545
    Vote Rating
    64
    Animal is a jewel in the rough Animal is a jewel in the rough Animal is a jewel in the rough

      -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
    4
    Vote Rating
    0
    brian.moeskau is an unknown quantity at this point

      -1  

    Default


    AJAX == Asynchronous JavaScript And Xml

  8. #8
    Ext User
    Join Date
    May 2009
    Posts
    3
    Vote Rating
    0
    jsaray is on a distinguished road

      0  

    Default synchronization issue

    synchronization issue


    Quote Originally Posted by cwolves View Post
    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
    Won't we have synchronization issues by using this approach ?, taking into account
    that myCallback code will be shared between many requests ?
    Thanks

  9. #9
    Ext User
    Join Date
    Jun 2007
    Posts
    3
    Vote Rating
    0
    danielv is on a distinguished road

      0  

    Default


    Quote Originally Posted by tryanDLS View Post
    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.
    I have a real need for either
    a) going synchronous
    b) manually blocking until the result of an aync call is available (which is more work than going synchronous and is ideologically equivalent to performing a synchronous call).

    We're trying to perform an operation which yields a decision during a drag event in a Tree, in order to determine if the current drag operation was successfully completed. If we go async, I need to block until the results of the AJAX call are returned in order to return the status of the drag.

    Perhaps you can suggest an approach which does not involve blocking, and can suspend the remainder of the event handlers until the result of an async call becomes available.

    In other words - how do I determine the subsequent handler currently bound to the event i am processing, conditionally over-ride it, and call it with the same parameters it was originally expecting without having to bend over backwards instead of simply making an exceptional use of the *evil* synchronous attribute already built in to the XmlRequest function?

  10. #10
    Sencha User
    Join Date
    Jul 2007
    Location
    Italy
    Posts
    134
    Vote Rating
    0
    fermo111 is on a distinguished road

      0  

    Default Sync for inclusion

    Sync for inclusion


    I agree with the these last posts. Sync/Asynch is a mechanism already built in XHR and it is there so that the programmer can chose what is best for the scenario he is implementing. Sync request have a very respectable name, RPC, and RPC have a long standing tradition. I cast my vote for the full support in Ext.

    Luca