简体   繁体   English

如何将WinJS ListView绑定到Web服务?

[英]How can I bind a WinJS ListView to a web service?

I want to connect to a web service and display results in a WinJS ListView. 我想连接到Web服务并在WinJS ListView中显示结果。 The web service returns JSON. Web服务返回JSON。 So far I have this markup: 到目前为止,我有这个标记:

    <div id="mediumListIconTextTemplate" data-win-control="WinJS.Binding.Template">
            <div>
                <h4 data-win-bind="innerText: title"></h4>
            </div>
    </div>

    <div id="basicListView" data-win-control="WinJS.UI.ListView"
        data-win-options="{itemDataSource : DataExample.itemList.dataSource, 
        itemTemplate: select('#mediumListIconTextTemplate')}">
    </div>

Here's what I think I need to do in JS: 这是我想在JS中要做的事情:

  • Use WinJS.xhr to get JSON data from the web service. 使用WinJS.xhr从Web服务获取JSON数据。
  • Create an array of data elements from the returned JSON 根据返回的JSON创建数据元素数组
  • Use WinJS.Binding.List to create a List from the array 使用WinJS.Binding.List从数组创建一个List

What am I missing? 我想念什么? Where do I call WinJS.Binding.processAll(my_listview, data_list) ? 在哪里调用WinJS.Binding.processAll(my_listview, data_list)

You do not need to call WinJS.Binding.processAll(my_listview, data_list). 您不需要调用WinJS.Binding.processAll(my_listview, data_list).

I will assume you are using the single-page nav model, so in the page's ready event handler, you would do the following: 我将假设您正在使用单页导航模型,因此在页面的ready事件处理程序中,您将执行以下操作:

var page = WinJS.UI.Pages.define("/pages/home.html", {
        ready: function (element, options) {

            WinJS.xhr({url:'http://someservice.com'}).then(
                function(response) { 
                    var json = JSON.parse(response.responseText);
                    var list = new WinJS.Binding.List(json.results); // or whatever array you are binding too 
                    DataExample.itemList = list; // or however you want to get the list into DataExample.itemList
                },
                function(error) {
                    //handle error 
                }
            );
        }

That's it. 而已。 default.js will handle the processAll() for the entire page, just let the WinJS magic do the work for you. default.js将处理整个页面的processAll(),只需让WinJS magic为您完成工作即可。

You can find a simple example of doing this at http://slickthought.net/post/2012/08/20/Windows-8-and-HTML-Part-6-Displaying-Data-with-WinJS-ListView.aspx . 您可以在http://slickthought.net/post/2012/08/20/Windows-8-and-HTML-Part-6-Displaying-Data-with-WinJS-ListView.aspx中找到一个简单的示例。 You can also follow the ListView QuickStart here http://msdn.microsoft.com/en-us/library/windows/apps/hh465496.aspx 您也可以在此处遵循ListView快速入门, 网址为http://msdn.microsoft.com/zh-cn/library/windows/apps/hh465496.aspx

If you are not using the single page model, then inside the activated handler on default.js you will just add the code above to execute after inside the promise returned by WinJS.UI.processAll() 如果您不使用单页模型,则在default.js的激活处理程序内,您只需在WinJS.UI.processAll()返回的promise中添加以上代码即可执行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM