locked
Appending Search Box suggestions, not using the Promise API

    Question

  • Hi,

    Before Windows 8.1, my app used the Search Charm to provide suggestions. I now want to provide suggestions with the Search Box (http://code.msdn.microsoft.com/windowsapps/SearchBox-control-sample-0f64f94d).

    This is the code i had before for the suggestion handler in Windows 8:

    var suggestionsRequestedHandler = function (eventObject) {
    	var queryText = eventObject.queryText;
    	var suggestions = eventObject.request.searchSuggestionCollection;
    	var deferral = eventObject.request.getDeferral();
    
    	if (!queryText || queryText == ""){
    		deferral.complete();
    		return;
    	}
    	window.geocodingController.getSuggestions({ queryText: queryText }, function (results) {
    		for (var i = 0, l = results.length; i < l; i++) {
    			var result = results[i];
    			var description = result.description;
    			suggestions.appendQuerySuggestion(result.description);
    		}
    		deferral.complete();
    	});
    };

    Now the API changed, you are no longer passed a deferral that you can call whenever you want. Instead, you have to give it a "Promise", like the one you get from an XHR call.

    But in my case, the suggestions are not grabbed by an XHR call (it is a little more complicated than that, I have to make a call from a JavaScript lib that I don't control, in an iFrame). So i would have to get the same behaviour, knowing that all I have is a callback that is called with a list of suggestions.

    How can i turn this callback into a Promise? Is there any way i can do something similar as what I was previously doing?

    Thanks a lot for your help,

    Fabien

    Sunday, November 17, 2013 4:40 PM

Answers

  • After digging some more, I seem to have found a way to make it work. Might not be the best but it works:

            var promise = new WinJS.Promise(function (complete, progress, error){
                window.geocodingController.getSuggestions({ queryText: queryText }, function (results) {
                    complete(results);
                });
            });
            eventObject.detail.setPromise(promise.then(function (results) {
                for (var i = 0, l = results.length; i < l; i++) {
                    var result = results[i];
                    var description = result.description;
                    suggestionCollection.appendQuerySuggestion(description);
    
                }
            }));

    Might be useful for someone else :)

    Fabien

    Sunday, November 17, 2013 5:15 PM

All replies

  • After digging some more, I seem to have found a way to make it work. Might not be the best but it works:

            var promise = new WinJS.Promise(function (complete, progress, error){
                window.geocodingController.getSuggestions({ queryText: queryText }, function (results) {
                    complete(results);
                });
            });
            eventObject.detail.setPromise(promise.then(function (results) {
                for (var i = 0, l = results.length; i < l; i++) {
                    var result = results[i];
                    var description = result.description;
                    suggestionCollection.appendQuerySuggestion(description);
    
                }
            }));

    Might be useful for someone else :)

    Fabien

    Sunday, November 17, 2013 5:15 PM
  • Great! Thanks for sharing the solution. :)
    Monday, November 18, 2013 1:49 AM
    Moderator