2013-11-03 35 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