2013-11-03 38 views
1

我想开发我的第一个Windows 8商店应用程序(HTML/JS)。我正在使用网格应用模板来满足我认为最好的需求。 这是我的模型:WinJS:加载数据

我有三个实体:1. GalleryCategory 2. Gallery 3. GalleryItem。 一个图库仅链接到一个类别。一个GalleryItem链接到一个图库...所以在这里没有什么特别的...

我使用开箱即用的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... 
        } 
       ); 
     }, 

我的问题是obivous ...就绪功能继续/检索数据之前结束......作为异步调用需要一段时间。

但我认为使用promise(.done())会为我做到这一点(同步线程)?或者我需要使用join()函数。如果是这样,在哪里以及如何?对不起,我的问题,这... ...

感谢您的帮助......

回答

1

就绪功能本身是一个异步功能,所以你只需要返回一个承诺,告诉它的调用者,它直到完成一些承诺已经解决。所以你可以用7个关键笔画修复你的问题。在拨打WinJS.xhr之前只需添加return

+0

很酷。有用。非常感谢你! :-) – Adatams