23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Here’s how it’s done in Scenario 2 of the Search contract sample (where suggestionList is just a<br />

hard-coded list of city names):<br />

Windows.ApplicationModel.Search.SearchPane.getForCurrentView().onsuggestionsrequested =<br />

function (eventObject) {<br />

var queryText = eventObject.queryText;<br />

var suggestionRequest = eventObject.request;<br />

var query = queryText.toLowerCase();<br />

var maxNumberOfSuggestions = 5;<br />

for (var i = 0, len = suggestionList.length; i < len; i++) {<br />

if (suggestionList[i].substr(0, query.length).toLowerCase() === query) {<br />

suggestionRequest.searchSuggestionCollection.appendQuerySuggestion(<br />

suggestionList[i]);<br />

if (suggestionRequest.searchSuggestionCollection.size ===<br />

maxNumberOfSuggestions) {<br />

break;<br />

}<br />

}<br />

}<br />

};<br />

So if query contains “ba” as it would in Figure 12-10, the first 5 names in suggestionList will be<br />

Bangkok, Bangalore, Baghdad, Baltimore, and Bakersfield. Of course, a real app will be drawing<br />

suggestions from its own database or from a service (simulated in Scenarios 5 and 6, by the way), but<br />

you get the idea. With a service, though, you should also check the suggestionResult.isCanceled<br />

property before starting a new request: this flag indicates that the search query hasn’t actually changed<br />

from a previous query and it’s not necessary to create new suggestions.<br />

Note When the SearchPane.searchHistoryEnabled property is true (the default), a user’s search<br />

history will be automatically tracked with prior searches appearing as suggestions when the search<br />

charm is first invoked (before the user types any other characters). Setting this property to false will<br />

disable the behavior, in which case an app can maintain its own history of previous queryText values. If<br />

an app does this, we recommend providing a means to clear the history through the app’s Settings.<br />

Apps can also use the SearchPane.searchHistoryContext property to create different histories<br />

depending on different contexts. When this value is set prior to the search charm being invoked,<br />

automatically managed search terms (searchHistoryEnabled is true) will be saved for that context.<br />

This has no effect when an app manages its own history, in which case it can manage different histories<br />

directly.<br />

Now the eventArgs.request property, a SearchPaneSuggestionsRequest object, has a few features<br />

you want to know about. Its searchSuggestedCollection property is unique—it’s not an array or other<br />

generic vector but a SearchSuggestionCollection object with a size property and four methods:<br />

appendQuerySuggestion (to add a single item to the list, as shown above), appendQuerySuggestions (to<br />

add an array of items at once, as you might receive from a query to a service), appendResultSuggestion<br />

(see next section) and appendSearchSeparator (which is used to group suggestions). In the latter case, a<br />

separator is given a label and appears as follows:<br />

522

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!