Results 1 to 6 of 6

Thread: Looping service call

  1. #1
    Sencha User
    Join Date
    May 2013
    Posts
    64
    Answers
    2

    Default Answered: Looping service call

    I am using ExtJS and Springs to develope application..
    Following ExtJS MVC pattern..

    Suppose I have an List of 10 Object. I want to loop and make service all by sending One object at a time. I want to send second object only when the service call for first object is successful..

    How can I loop and make the service call..?

  2. Create an array of objects to send, pop one off the stack and make an ajax request.

    In your callback, look @ the stack and if length > 0, pop the next one off and so on. If the length is 0, then you've reached the end.

  3. #2
    Sencha User jay@moduscreate.com's Avatar
    Join Date
    Mar 2007
    Location
    DC Area =)
    Posts
    16,364
    Answers
    102

    Default

    Create an array of objects to send, pop one off the stack and make an ajax request.

    In your callback, look @ the stack and if length > 0, pop the next one off and so on. If the length is 0, then you've reached the end.

  4. #3
    Sencha User
    Join Date
    May 2013
    Posts
    64
    Answers
    2

    Default

    Thanks Jay.. Got an idea...

  5. #4
    Sencha User
    Join Date
    May 2013
    Posts
    64
    Answers
    2

    Default

    Jay,
    Popping up from stack makes sure we send all objects one by one.. But what about the response???
    I want to send send object only when first object response is successful..

  6. #5
    Sencha Premium User evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    19,258
    Answers
    759

    Default

    The process is exactly the same:

    Code:
    function doRequest(items) {
        if (items.length) {
            Ext.Ajax.request({
                url: 'foo',
                params: {
                    id: items.shift()
                },
                success: function() {
                    // do stuff
                    doRequest(items);
                }
            });
        }
    }
    
    var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
    doRequest(arr);
    Twitter - @evantrimboli
    Former Sencha framework engineer, available for consulting.
    As of 2017-09-22 I am not employed by Sencha, all subsequent posts are my own and do not represent Sencha in any way.

  7. #6
    Sencha User
    Join Date
    May 2013
    Posts
    64
    Answers
    2

    Default

    Awesome.. This example code is perfect... Thank you very much...

Posting Permissions

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