Windows 8 WinJS.UI.Pages.IPageControlMembers方法中的完整函数

Windows 8 WinJS.UI.Pages.IPageControlMembers方法中的完整函数,windows-8,windows-store-apps,winjs,Windows 8,Windows Store Apps,Winjs,如何在调用准备方法之前完成init中的函数。我的代码: WinJS.Namespace.define("Data", { source: "" }); var page = WinJS.UI.Pages.define("/html/page.html", { init: function (element, options) { createDataSoucre(); }, ready: function () { docume

如何在调用准备方法之前完成init中的函数。我的代码:

WinJS.Namespace.define("Data", {
    source: ""
});

var page = WinJS.UI.Pages.define("/html/page.html", {
    init: function (element, options) {
        createDataSoucre();
    },

    ready: function () {
        document.getElementById("result").innerHTML = Data.source;
    }
});

function createDataSoucre() {
    //blah blah (calculate thousands of calculations)
    Data.source = result;
}
当我运行时,页面不会呈现“result”标记。我尝试使用承诺,但它对我不起作用:

init: function (element, options) {
        return new WinJS.Promise.as(createDataSoucre());
}

谢谢您的时间。

我在一个简单的测试项目中尝试了您的代码,如下所示:

(function () {
    "use strict";

    WinJS.Namespace.define("Data", {
        source: ""
    });

    function createDataSource() {        
        Data.source = "<ul><li>Item 1</li><li>Item2</li><li>Item3</li></ul>";
    }

    WinJS.UI.Pages.define("/pages/home/home.html", {
        init: function (element, options) {
            createDataSource();
        },

        ready: function (element, options) {
            document.getElementById("result").innerHTML = Data.source;
        }
    });
})();
因为calculateIntegerSum返回一个承诺并实现为异步,所以createDataSource将返回一个承诺,您可以从init返回:

init: function (element, options) {
    return createDataSource();
},
我在一个项目中尝试了这个方法,效果很好,页面加载等待计算完成

function createDataSource() {
    return calculateIntegerSum(100000, 2);
}
init: function (element, options) {
    return createDataSource();
},