weixin_33728708 2017-06-22 18:47 采纳率: 0%
浏览 47

$ .whenall与Ajax

I have a function for returning a feed which is retrieved by an AJAX-Call and now want to do something after a few of these asynchron requests have been done.

doit() is the function I call at first.

I am sorry for not providing a url, but it is an internal server.

Here is my code:

   function grabFollowedCommunityPageFeed(page, cCallback) {
                $.ajax({
                    url: "blabla.com&page=" + page,
                    method: "GET",
                    contentType: "application/atom+xml"
                }).always(function(xhr, ignore, thrownMessage) {
                    var totalResults = 0;
                    if ((200 === thrownMessage.status) && (xhr)) {
                        totalResults = parseInt($(xhr).find("totalResults").first().text()) || -1;
                    }
                    if (cCallback && $.isFunction(cCallback)) {
                        cCallback({feed: xhr, resultCount: totalResults});
                    }
                });
            }

            function grabFollowedCommunitiesFeeds(pagecount) {
                var i = 1,
                    deferredArr = [];
                for (i = 1; i < pagecount; i += 1) {
                    grabFollowedCommunityPageFeed(i, function callback(resultObj) {
                        deferredArr[i] = new $.Deferred();
                        deferredArr[i].resolve(resultObj);
                    });
                }
                return deferredArr;
            }


            function doit() {
                var allCommunityFeedObjects = [],
                    allCommunityFeedObjectsCount = 0,
                    deferredObj = [];
                (function initialReadFollowedCommunityFeedPages() {
                    grabFollowedCommunityPageFeed(1, function(requestObj) {
                        allCommunityFeedObjectsCount = requestObj.resultCount;
                        var tEntries = $(requestObj.communityFeed).find("entry"),
                            el$;
                        $.each(tEntries, function(ignore, el) {
                            el$ = $(el);
                            if (!($.inArray(el$, allCommunityFeedObjects) !== -1)) {
                                allCommunityFeedObjects.push(el$);
                            }
                        });

                        deferredObj = grabFollowedCommunitiesFeeds(allCommunityFeedObjectsCount) || [];
                        $.whenAll.apply($, deferredObj).always(function(allCommunityFeeds) {
                            var k = allCommunityFeeds;
                            // union k with allCommunityFeedObjects
                        });
                    });
                })();

            }

This line seems to be fine as well and I have checked it:

deferredArr[i].resolve(resultObj);

The problem is that allCommunityFeeds parameter is undefined in

  $.whenAll.apply($, deferredObj).always(function(allCommunityFeeds) 

and that means there is something wrong. Can you help me?

  • 写回答

0条回答 默认 最新

    报告相同问题?