Search Contract - Async? RRS feed

  • General discussion

  • When I right-click on my WinJS project and choose add->new item->search contract it generates a shell of a page, however the boilerplate code has searchData as a synchronous function, which is puzzling given so much of the emphasis on the platform is async.  

    Do people just rewrite this boilerplate altogether, or is there a simpler workaround for this?



    Tuesday, May 1, 2012 1:14 AM

All replies

  • If you return a promise of a WinJS.Binding.List in your searchData function, you can make the rest of the code work by replacing the handleQuery function with the following:

    handleQuery: function (element, eventObject) {
                this.lastSearch = eventObject.queryText;
                WinJS.Namespace.define("search", { markText: this.markText.bind(this) });
                this.updateLayout(element, Windows.UI.ViewManagement.ApplicationView.value);
                var that = this;
                this.searchData(eventObject.queryText).then(function (originalResults) {
                    that.populateFilterBar(element, originalResults);
                    that.applyFilter(that.filters[0], originalResults);

    Friday, May 18, 2012 12:32 PM
  • Search is just an event. It activates your app, hands you the queryText, and then trusts you to do something useful for the user (it doesn't expect any kind of return from your handler function). In other words: once your querySubmitted handler function has the queryText you can then go do something synchronous, async, or whatever.

    Friday, May 18, 2012 6:46 PM