1

在我的 Win 8 应用程序中,基于空白模板,我已成功添加搜索合同,尽管我尚未将其链接到任何数据,但它似乎可以工作,所以,现在,当我在我的应用程序中搜索任何术语时它只是将我带到带有“未找到结果”消息的 searchResults 页面,这是我最初所期望的。

现在我想做的是将我的数据库链接到 searchResults.js 文件中,以便我可以查询我的数据库。现在在搜索合同之外,我已经测试并连接了我的 Db 并且它可以工作;我使用WinJS.xhr, 连接到我的网络服务,该服务反过来查询我的数据库并返回一个 JSON 对象。

在我的测试中,我只对 url 进行了硬编码,但是我现在需要做两件事。将用于连接我的数据库的测试WinJS.xr数据移动到搜索合同代码中,然后 - 将硬编码的 url 更改为接受用户搜索词的动态 url。

目前我对Win 8搜索的了解,搜索合约的实际数据查询部分如下:

// This function populates a WinJS.Binding.List with search results for the provided query.
    _searchData: function (queryText) {
        var originalResults;
        // TODO: Perform the appropriate search on your data.
        if (window.Data) {
            originalResults = Data.items.createFiltered(function (item) {
                return (item.termName.indexOf(queryText) >= 0 || item.termID.indexOf(queryText) >= 0 || item.definition.indexOf(queryText) >= 0);
            });
        } else {`enter code here`
            originalResults = new WinJS.Binding.List();
        }
        return originalResults;
    }
});

我需要转移到本节的代码如下;现在我不得不承认我目前不理解上面的代码块,也没有找到一个好的资源来逐行分解它。如果有人可以提供帮助,那将是非常棒的!我下面的代码,我基本上是想整合它,然后让 searchString 等于用户搜索词。

   var testTerm = document.getElementById("definition");
    var testDef = document.getElementById("description");

    var searchString = 2;
    var searchFormat = 'JSON';

    var searchurl = 'http://www.xxx.com/web-service.php?termID=' + searchString +'&format='+searchFormat;

    WinJS.xhr({url: searchurl})
      .done(function fulfilled(result)

      {
          //Show Terms                 
          var searchTerm = JSON.parse(result.responseText);

          // var terms is the key of the object (terms) on each iteration of the loop the var terms is assigned the name of the  object key
          // and the if stament is evaluated

          for (terms in searchTerm) {

              //terms will find key "terms"
              var termName = searchTerm.terms[0].term.termName;
              var termdefinition = searchTerm.terms[0].term.definition;

              //WinJS.Binding.processAll(termDef, termdefinition);
              testTerm.innerText = termName;
              testDef.innerText = termdefinition;
          }              
    },
              function error(result) {
                  testDef.innerHTML = "Got Error: " + result.statusText;
              },
              function progress(result) {
                  testDef.innerText = "Ready state is " + result.readyState;
              });          
4

1 回答 1

1

我将尝试为您不太理解的代码段提供一些解释。我相信您上面的代码来自 Visual Studio 添加的默认代码。请参阅注释中的解释。

/**
 * This function populates a WinJS.Binding.List with search results 
 * for the provided query by applying the a filter on the data source
 * @param {String} queryText - the search query acquired from the Search Charm
 * @return {WinJS.Binding.List} the filtered result of your search query.
 */
_searchData: function (queryText) {
    var originalResults;
    // window.Data is the data source of the List View 
    // window.Data is an object defined in YourProject/js/data.js
    // at line 16 WinJS.Namespace.define("Data" ...
    // Data.items is a array that's being grouped by functions in data.js
    if (window.Data) {
        // apply a filter to filter the data source
        // if you have your own search algorithm, 
        // you should replace below code with your code
        originalResults = Data.items.createFiltered(function (item) {
            return (item.termName.indexOf(queryText) >= 0 ||
                    item.termID.indexOf(queryText) >= 0 || 
                    item.definition.indexOf(queryText) >= 0);
            });
    } else {
        // if there is no data source, then we return an empty WinJS.Binding.List
        // such that the view can be populated with 0 result
        originalResults = new WinJS.Binding.List();
    }
    return originalResults;
}

由于您正在考虑在自己的 Web 服务上进行搜索,因此您始终可以使您的_searchData函数异步并让您的视图等待从您的 Web 服务返回的搜索结果。

_searchData: function(queryText) {
    var dfd = new $.Deferred();
    // make a xhr call to your service with queryText
    WinJS.xhr({
        url: your_service_url,
        data: queryText.toLowerCase()
      }).done(function (response) {
           var result = parseResultArrayFromResponse(response);
           var resultBindingList = WinJS.Binding.List(result);
           dfd.resolve(result)
      }).fail(function (response) {
          var error = parseErrorFromResponse(response);
          var emptyResult = WinJS.Binding.List();
          dfd.reject(emptyResult, error);
      });
    return dfd.promise();
}
...
// whoever calls searchData would need to asynchronously deal with the service response.

_searchData(queryText).done(function (resultBindingList) {
    //TODO: Display the result with resultBindingList by binding the data to view
}).fail(function (resultBindingList, error) {
    //TODO: proper error handling
});
于 2012-12-20T07:25:21.807 回答