Skip to content

Instantly share code, notes, and snippets.

@alegut
Created July 15, 2018 10:07
Show Gist options
  • Select an option

  • Save alegut/b520b4f2139a409d28e1615c9ed2ed47 to your computer and use it in GitHub Desktop.

Select an option

Save alegut/b520b4f2139a409d28e1615c9ed2ed47 to your computer and use it in GitHub Desktop.
Asynchtonous JSON request
getResults() {
$.when(
$.getJSON(universityData.root_url + '/wp-json/wp/v2/posts?search=' + this.searchField.val()),
$.getJSON(universityData.root_url + '/wp-json/wp/v2/pages?search=' + this.searchField.val())
).then((posts, pages) => {
var combinedResults = posts[0].concat(pages[0]);
this.resultsDiv.html(`
<h2 class="search-overlay__section-title">General Information</h2>
${combinedResults.length ? '<ul class="link-list min-list">' : '<p>No general information matches that search.</p>'}
${combinedResults.map(item => `<li><a href="${item.link}">${item.title.rendered}</a></li>`).join('')}
${combinedResults.length ? '</ul>' : ''}
`);
this.isSpinnerVisible = false;
}, () => {
this.resultsDiv.html('<p>Unexpected error; please try again.</p>');
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment