0

The application has to request a web service by $.ajax, but the times would be multiple, and it depends on the user. Since it's dynamic, it seems that $.when can't work for it.

Here is the code:

var increase=0;
    var test_use_upload_file_name= t2_image_path+ increase;
var soap_add_new_story_image=
    '<?xml version="1.0" encoding="utf-8"?>' +
    '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' + 
            'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '+
            'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'+
                '<soap:Body>'+
                    '<AddNewStoryImage xmlns="http://x.x.x.x/StoryForMac/">'+
                        '<StoryID>'+story_id+'</StoryID>'+
                        '<UserName>'+User_Name+'</UserName>'+
                        '<DragNumber>'+Drag_Number+'</DragNumber>'+
                        '<ImagePath>'+test_use_upload_file_name+'</ImagePath>'+
                    '</AddNewStoryImage>'+
                '</soap:Body>'+
            '</soap:Envelope>';   


//multiple ajax request
var start= 0;
for(;start< **USER_Decide**;start++)
{
    $.ajax({
        type: "POST",
        url: webServiceAddNewStoryImgUrl,
        contentType: "text/xml",
        dataType: "xml",
        data: soap_add_new_story_image,     
        success: process_add_new_img_Success,
        error: process_add_new_img_Error        
    }); 
    increase++;
    test_use_upload_file_name= t2_image_path+ increase; 
}

Since I don't know how much pic the user will draw, I have to update the file name each time(increase++).
Please any kind of suggestion~ Thanks!

UPDATE: Sorry for my poor expression. This code WON"T WORK. My problem is similar to this Please take a look. But the difference is that I don't know how many times I have to call the ajax request, because the user will determine it. Thus, I can't use the method $.when provided here. Is it clear?...

Community
  • 1
  • 1
Alston
  • 2,085
  • 5
  • 30
  • 49
  • So what exactly is the issue? – VIDesignz Nov 04 '12 at 06:15
  • u want a better model for this? if u have a set of html tags all having same class and storing the image paths, u can loop through those tags using a `for each` loop and get the image url from the tag currently being checked in the loop. – redDevil Nov 04 '12 at 06:34
  • Please check the updated notification. – Alston Nov 04 '12 at 06:53
  • Actually, I believe (if I understand your use case correctly) that you can use `when`, using `apply`. see [here](http://stackoverflow.com/questions/10345124/whats-the-meaning-of-when-applynull-a-method-in-jquery). – MasterAM Nov 04 '12 at 07:21
  • @MasterAM I'll try on it. Now I'm trying to solve it by recursive call: if the $.ajax call succeeded, and the method process_add_new_img_Success will fire to call the $.ajax again. Trying... – Alston Nov 04 '12 at 07:38
  • The recursive method finally works... – Alston Nov 04 '12 at 09:21
  • @MasterAM I will still try on it but later... – Alston Nov 04 '12 at 09:25
  • @MasterAM Sorry, it can't work. – Alston Nov 17 '12 at 10:08

1 Answers1

0

Check this, one one possible methods to acheive your goal http://jsfiddle.net/oceog/mNegP/

var object = {
    a: {
        id: 'aaa',
        text: 'bbb'
    },
    b: {
        id: 'ccc',
        text: 'cccc'
    }
};
var json = JSON.stringify(object);
console.log("JSON:", json);

//that will run callback when counter is zero
var check_success = function(num, callback) {
    console.log("one done",num,"left");
    if (num <=0) {
        callback();
    }
}

var checkloop = function() {
        var done=0;
      var num=10;// (decide by user);
//this will be called only if all requests done
var run_onlast=function() {
    console.log("last done");
    };
    //at this point we know how many ajax we would run in row
    for (var i = 0; i <= num; i++) {
        console.log("start",i);
        $.post('/echo/json/', {
        //just for sample, random delays
            json: json,delay: Math.round(Math.random()*4)
        },  "json").success(function(data) {
        //run our check function provide it with our last function and our callback
            check_success(num,run_onlast)
        }).error(function(data) {

            console.log({
                error: data.responseText
            });
        }).complete(function() {
        //always reduce, even if error
            num--;
        });
    }
        done=1;
}
    
    checkloop();
​
zb'
  • 8,071
  • 4
  • 41
  • 68
  • Although I can address this issue with recursive calling method(my comment above.), I will still give it a try. If it really can work and no other better solution appears, I will choose it as the answer. Thank u. – Alston Nov 04 '12 at 09:23
  • as i understand with recursive, you will run all queries one by one, – zb' Nov 04 '12 at 09:32