I’am working my way through the Windows 8 search contract functionality, in particular the default searchResults.js file created when you add the search contract to your project. My issue occurs when I try and search my app when it is either in a suspended or terminated state i.e not the main window.
The code for this is as follows:
// Handles when a users performs a search and the app is not already the main app
WinJS.Application.addEventListener("activated", function (args) {
//If activation kind is search...
if (args.detail.kind === appModel.Activation.ActivationKind.search) {
args.setPromise(ui.processAll().then(function () {
// If there is no current navigation location
if (!nav.location) {
// Declare object
nav.history.current = { location: Application.navigator.home, initialState: {} };
}
// Else navigate to searchPageURI,
return nav.navigate(searchPageURI, { queryText: args.detail.queryText });
}));
}
});
I have added the comments myself to try and breakdown the code. Now I understand the premise, this code checks to see how the app was activated and runs two different processes depending on whether their is an existing navigation location or not. However when I run the app and search from outside I get an error that is: JavaScript runtime error: 'Application' is undefined. It is referring to the line:
nav.history.current = { location: Application.navigator.home, initialState: {} };
Can someone explain why this is happening and what I have defined, or where I should define it?
It appears that this is an issue that only happens when you use add the built-in “Search Contract” with a blank project. If you add the search contract with the other types of project templates it apparently works.
My solution to fixing it in my blank template was to remove
Application., leaving justNavigator.home. I have to admit that I don’t fully understand exactly why this works, however I was able to compile my project again once I had done this.If someone could update this with an explanation of why that works I’m sure that it may be helpful in future for other developers as Win 8 development picks up.