在我的Win 8应用程序中,基于一个空白模板,我已经成功添加了搜索合同,但它似乎仍然有用,尽管我还没有将它链接到任何数据,所以,现在,当我搜索任何一个术语时在我的应用程序中,它只是将我带到searchResults页面,并显示消息“找不到结果”这是我最初的期望。
现在我想要做的是将我的数据库链接到searchResults.js文件,以便我可以查询我的数据库。现在在搜索合同之外我已经测试并连接了我的Db并且它有效;我使用WinJS.xhr
完成此操作,以连接到我的Web服务,后者又查询我的数据库并返回一个JSON对象。
在我的测试中,我只对硬编码进行了硬编码,但是我现在需要做两件事。将用于连接我的数据库的测试WinJS.xr
数据移动到搜索合同代码中,然后将硬编码的网址更改为接受用户搜索字词的动态网址。
根据我对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;
});
答案 0 :(得分: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
});