Javascript 在Win 8应用程序搜索合同中集成到我的数据库的链接

Javascript 在Win 8应用程序搜索合同中集成到我的数据库的链接,javascript,windows-8,Javascript,Windows 8,在我的Win 8应用程序中,基于一个空白模板,我成功地添加了搜索合同,尽管我还没有将其链接到任何数据,但它似乎仍然有效。因此,现在,当我在我的应用程序中搜索任何术语时,它只会将我带到搜索结果页面,并显示消息“未找到结果”,这是我最初期望的结果 现在我想做的是将我的数据库链接到searchResults.js文件中,以便查询我的数据库。现在,在搜索合同之外,我已经测试并连接了我的数据库,它可以正常工作;我使用WinJS.xhr连接到我的web服务,web服务反过来查询我的数据库并返回一个JSON对

在我的Win 8应用程序中,基于一个空白模板,我成功地添加了搜索合同,尽管我还没有将其链接到任何数据,但它似乎仍然有效。因此,现在,当我在我的应用程序中搜索任何术语时,它只会将我带到搜索结果页面,并显示消息“未找到结果”,这是我最初期望的结果

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

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

   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;
              });          
根据我对Win 8 search的了解,到目前为止,搜索合同的实际数据查询部分如下:

// 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;
              });          

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

/**
 * 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服务上进行搜索,因此始终可以使您的
\u 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
});

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

/**
 * 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服务上进行搜索,因此始终可以使您的
\u 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
});