我正在尝试开发我的第一个 Windows 8 应用商店应用程序 (HTML/JS)。我正在使用网格应用程序模板,我认为它最适合我的需求。 这是我的模型:
我有三个实体:1. GalleryCategory 2. Gallery 3. GalleryItem。 画廊仅链接到一个类别。 GalleryItem 恰好链接到一个 Gallery...所以这里没什么特别的...
我正在使用开箱即用的 data.js 文件在应用程序启动时加载所有类别和所有画廊。但是当我打开 galleryDetail.html (应该显示特定图库的所有图像)时,我想加载图库的所有图像。 (以避免一开始就加载太多)。 现在我终于说到了我不明白的地方:
我该如何处理这个问题?我的意思是
WinJS.UI.Pages.define("/pages/galleryDetail/galleryDetail.html", {
// This function is called whenever a user navigates to this page. It
// populates the page elements with the app's data.
ready: function (element, options) {
var item = options && options.item ? Data.resolveItemReference(options.item) : Data.items.getAt(0);
element.querySelector(".titlearea .pagetitle").textContent = item.group.title;
element.querySelector("article .item-title").textContent = item.title;
element.querySelector("article .item-subtitle").textContent = item.subtitle;
element.querySelector("article .item-image").src = item.backgroundImage;
element.querySelector("article .item-image").alt = item.subtitle;
element.querySelector("article .item-content").innerHTML = item.content;
element.querySelector(".content").focus();
var galleryId = item.key;
WinJS.xhr({ url: "http://someUrlToAnAspNetWebsite/Handlers/GalleryItemsHandler.ashx?galleryId=" + galleryId }).done(
// Complete function
function (response) {
var items = JSON.parse(response.responseText);
items.forEach(function (item) {
galleryItemsList.push(item);
});
dataList = new WinJS.Binding.List(galleryItemsList);
var galleryItemsListView = document.getElementById('galleryItemsListView').winControl;
galleryItemsList.itemDataSource = dataList.dataSource;
},
// Error function
function (response) {
// handle error here...
},
// Progress function
function (response) {
// progress implementation goes here...
}
);
},
我的问题很明显...准备好的函数在检索数据之前继续/结束...因为异步调用需要一段时间。
但我认为使用 promise (.done()) 将为我做到这一点(同步线程)?或者我是否需要使用 join() 函数。如果是的话,在哪里以及如何?抱歉,我对此有疑问...
感谢您的帮助...
最佳答案
ready 函数本身是一个异步函数,因此您只需返回一个 Promise 来告诉调用者在某些 Promise 得到解决之前它还没有完成。因此,您只需按 7 次按键即可解决您的问题。只需在 WinJS.xhr
调用之前添加 return
即可。
关于javascript - WinJS:加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19754754/