我正在使用 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/