等待初始化,直到异步加载数据

时间:2013-11-22 22:34:36

标签: javascript asynchronous windows-8

我正在尝试设计一个个人应用程序,它以异步方式加载数据,然后根据Windows 8.1商店应用程序显示网格。

我遇到了我的ui在加载数据之前试图执行的问题。

我目前的代码:

(function () {
"use strict";
var asyncInProgress = true;
var groupedItems;
var list;
var observable;
var matches = new WinJS.Binding.List();
var matchGroups = new WinJS.Binding.List();
var BattleGrounds = new WinJS.Binding.List();

list = getData();
initGroups(list);

function initGroups(l) {
    var groupedItems = list.createGrouped(
        function groupKeySelector(item) { return item.group.key; },
        function groupDataSelector(item) { return item.group; }
    );
}

WinJS.Namespace.define("Data", {
    Observable: WinJS.Class.define(function () {
        this.dispatch = function () {
            this.dispatchEvent("dataReady");
        }
    }),
    getObservable: getObservable,
    items: groupedItems,
    groups: groupedItems.groups,
    getItemReference: getItemReference,
    getItemsFromGroup: getItemsFromGroup,
    resolveGroupReference: resolveGroupReference,
    resolveItemReference: resolveItemReference,
    updateData: updateData,
    getAsyncStatus: getAsyncStatus
});

WinJS.Class.mix(Data.Observable, WinJS.Utilities.eventMixin);
WinJS.Class.mix(Data.Observable, WinJS.Utilities.createEventProperties("dataReady"));

// Provides support for event listeners.
function getObservable() {
    observable = new Data.Observable();
    return observable;
}

// Get a reference for an item, using the group key and item title as a
// unique reference to the item that can be easily serialized.
function getItemReference(item) {
    return [item.group.key, item.title, item.backgroundImage];
}

// This function returns a WinJS.Binding.List containing only the items
// that belong to the provided group.
function getItemsFromGroup(group) {
    return list.createFiltered(function (item) { return item.group.key === group.key; });
}

// Get the unique group corresponding to the provided group key.
function resolveGroupReference(key) {
    return groupedItems.groups.getItemFromKey(key).data;
}

// Get a unique item from the provided string array, which should contain a
// group key and an item title.
function resolveItemReference(reference) {
    for (var i = 0; i < groupedItems.length; i++) {
        var item = groupedItems.getAt(i);
        if (item.group.key === reference[0] && item.title === reference[1]) {
            return item;
        }
    }
}

function updateData() {
    asyncInProgress = true;
    BattleGrounds.splice(0, matches.length);
    BattleGrounds._currentKey = 0;
    groupedItems = null;
    list = getData();
    initGroups(list);
}

function getAsyncStatus() {
    return asyncInProgress;
}

function getData() {
    var darkGray = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXY3B0cPoPAANMAcOba1BlAAAAAElFTkSuQmCC";
    var lightGray = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXY7h4+cp/AAhpA3h+ANDKAAAAAElFTkSuQmCC";
    var mediumGray = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXY5g8dcZ/AAY/AsAlWFQ+AAAAAElFTkSuQmCC";

    var url = 'https://api.guildwars2.com/v1/wvw/matches.json';

    acquireSyndication(url).then(function (response) {

        // Remove any invalid characters from JSONp response.
        var fixedResponse = response.responseText.replace(/\\'/g, "'");
        var jsonObj = JSON.parse(fixedResponse);

        jsonObj.wvw_matches.forEach(function (battle) {
            var anet_id = value.wvw_match_id;
            // Create Group
            var matchGroup = {
                key: anet_id,
                title: anet_id
            };

            matchGroups.push(matchGroup);

            // Get Details
            acquireSyndication("https://api.guildwars2.com/v1/wvw/match_details.json?match_id=" + anet_id).then(function (json) {
                var fixedJson = json.responseText.replace(/\\'/g, "'");
                var obj = JSON.parse(fixedJson);
                fixedJson.maps.forEach(function (value) {
                    BattleGrounds.push({
                        group: matchGroup, key: matchGroup.title, title: value.type,
                        subtitle: value.type, map: "eb", description: "NA", content: "NA", "type": value.type,
                        "scores": value.scores, "objectives": value.objectives, "bonuses": value.bonuses, backgroundImage: lightGray
                    });
                });
            }, function (error) {

                var x = error.getAllResponseHeaders();
                var matchGroup = matchGroups[0];

                for (var i = 0; i < matchGroups.length; i++) {
                    flickrPosts.push({
                        group: matchGroups[i], key: matchGroup.title, title: "Error loading",
                        subtitle: "Error", backgroundImage: lightGray, published: "N/A", description: "N/A"
                    });
                }

                asyncInProgress = false;
                observable.dispatch();

            });
        });

    }, function (error) {

        var x = error.getAllResponseHeaders();
        var matchGroup = matchGroups[0];

        for (var i = 0; i < matchGroups.length; i++) {
            flickrPosts.push({
                group: matchGroups[i], key: matchGroup.title, title: "Error loading",
                subtitle: "Error", backgroundImage: lightGray, published: "N/A", description: "N/A"
            });
        }

        asyncInProgress = false;
        observable.dispatch();

    });
    return BattleGrounds;
}

function acquireSyndication(url) {
    return WinJS.xhr({
        url: url,
        headers: { "If-Modified-Since": "Mon, 27 Mar 1972 00:00:00 GMT" }
    });
}
})();

这组错误:groupsItems.groups。这说组是不确定的。 我知道这是因为数据仍在处理中。

我将如何解决这个问题? 我看了一下promise对象,但整个概念让我感到困惑,因为我对Windows 8应用程序的基础结构了解不够。

1 个答案:

答案 0 :(得分:1)

您的问题的核心在于getData()函数 - 它不会返回您的数据,因为它使用异步调用来获取数据。返回时数据尚未可用。似乎该函数进行了几次异步调用以获取数据(使用acquireSyndication())。当这些异步函数在将来的某个时间完成时,您可以在调用matchGroups之后将该数据放入BattleGrounds,然后再放入acquireSyndication()

你正在做的事情非常混乱,所以没有一个简单的解决办法。从概念上讲,您需要处理来自异步代码的完成处理程序的BattleGrounds数据,并且使用它的所有代码必须从该完成处理程序内部继续,而不是在getData()调用之后。您无法调用getData()并将其用作同步函数,因为它是异步的。这需要异步编程技术。

如果您正在进行多次异步调用并尝试在所有这些调用完成后执行某些操作(我认为这就是您正在做的事情),那么您还需要专门针对该条件进行编码。您既可以使用promises,也可以保留一个计数器,查看有多少个ajax调用,并在每个完成函数中,增加计数器,看看这是否是刚刚完成的最后一个,如果是,那么你可以处理所有数据并继续执行其余代码。

我还建议你不要在函数的一个部分使用promises,然后在下一部分完成回调。使用其中一种而不是混合物来保持代码清洁。