microsoft-metro - 当异步回调返回时,我如何知道我仍在正确的页面上?

标签 microsoft-metro winjs

我正在使用 single-page navigation model 构建 Metro 应用程序。在我的一个页面上,我启动一个异步 ajax 请求来获取一些信息。当请求返回时,我想将收到的信息插入到显示的页面中。

例如:

WinJS.UI.Pages.define("/showstuff.html", {
    processed: function (element, options) {
        WinJS.xhr(...).done(function (result) {
            element.querySelector('#target').innerText = result.responseText;
        });
    }
};

但是我怎么知道用户在此期间没有离开该页面呢?尝试在不同的页面上插入文本是没有意义的,那么我如何确保请求启动时正在加载的页面仍然处于事件状态?

最佳答案

您可以将页面 URI 与当前的 WinJS.Navigation.location 进行比较,以检查您是否仍在该页面上。您可以使用 Windows.Foundation.Uri 从页面 URI 中提取路径来执行此操作。

WinJS.UI.Pages.define("/showstuff.html", {
    processed: function (element, options) {
        var page = this;

        WinJS.xhr(...).done(function (result) {
            if (new Windows.Foundation.Uri(page.uri).path !== WinJS.Navigation.location)
                return;

            element.querySelector('#target').innerText = result.responseText;
        });
    }
};

关于microsoft-metro - 当异步回调返回时,我如何知道我仍在正确的页面上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11270270/

相关文章:

c# - 使用文件选择器 Metro 应用程序选择所有文件 Windows 8

javascript - WinJS listview itemtemplate 导航到另一个页面

javascript - WinJS,从一个可能异步也可能不异步的函数返回一个 promise

winjs - 使简单的 WinJS 应用程序默认为特定大小

javascript - MobileServices.web.js 未经授权的 api 调用

angularjs - Apache Cordova 后退按钮事件未在 Windows Phone 上触发(通用)

windows-8 - 如何防止/禁用 Windows 8 Metro UI 应用程序的快照 View

windows-runtime - Metro 应用程序 StorageFile GetThumbnailAsync 背景

visual-studio-2010 - 此安装不支持项目 {BC8A1FFA-BEE3-4634-8014-F334798102B3}

c# - 分组项目页 : View jumps back to start when navigating back from detailpage