locked
Ajax run part of code (a defer)before it moves onto the next part. RRS feed

  • Question

  • User-544325736 posted

    I have some code that is being called and it goes through the beginning of my $.ajax() I need this to go through my whole method before it exits and goes to the next part because it is not put together properly until it runs all the way through. Currently it starts and then exits skipping my defer and goes into the if (woimageindex >= 0) condition

    Here is my code that I have. I need if (woimageindex == -1) to run and go through defer.done(function () { but it skips everything in this section. How can I make it do this before moving on?

    if (woimageindex == -1)//item not found
                            {
                                var ajaxRequests = [];
    
                                url = rootPath + "utility/GetWebRequestImages";                          
    
                                ajaxRequests.push($.ajax({
                                    type: 'POST',
                                    url: url,
                                    data: {
                                         corp_id: WOCorp_id
                                    },
                                    dataType: 'json'
                                }));
    
                                var defer = $.when.apply($, ajaxRequests);
                                defer.done(function () {
                                    // This is executed only after every ajax request has been completed
                                    $.each(arguments, function (index, responseData) {
                                        switch (index) {
                                            case 0://status
                                                //populate the AutoCompleteDataSource with the data from server
                                                arrWOWebRequestImagesList.push(new PickListModel(WOCorp_id, responseData));
                                                woimageindex = arrWOWebRequestImagesList.length - 1;
                                                break;
                                        }
                                        //alert(index);
                                    });//end of switch
                                });//end of defer
                            }
                        
                            if (woimageindex >= 0)
                            {
                                var categories = arrWOWebRequestImagesList[woimageindex].list;
                                alert(categories.length);
                                var list = [];
    
                                // added new PickListModel(webimage_id, after .push(
                                    //list.push(new PickListModel(webimage_id, $.map(arrWOWebRequestImagesList[woimageindex].list, function (item) {
                                list.push( $.map(arrWOWebRequestImagesList[woimageindex].list, function (item) {
                                    return new PickListItem(item.caption, item.caption, item.webimage_id);
                                }));
    

    Tuesday, May 14, 2019 6:47 PM

All replies